-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ddanilyuk/feature/scheduleKPI-API
Add V2 Rozklad API
- Loading branch information
Showing
14 changed files
with
352 additions
and
32 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// GroupsControllerV2.swift | ||
// | ||
// | ||
// Created by Denys Danyliuk on 01.07.2022. | ||
// | ||
|
||
import Vapor | ||
import Routes | ||
import KPIHubParser | ||
import Foundation | ||
|
||
final class RozkladControllerV2 { | ||
|
||
func allGroups(request: Request) async throws -> GroupsResponseV2 { | ||
let response: ClientResponse = try await request.client.get( | ||
"https://schedule.kpi.ua/api/schedule/groups" | ||
) | ||
let result = try response.content.decode(GroupModelV2ClientResponse.self) | ||
return GroupsResponseV2( | ||
numberOfGroups: result.data.count, | ||
groups: result.data.sorted(by: { | ||
$0.name.compare($1.name, locale: Locale(identifier: "uk")) == .orderedAscending | ||
}) | ||
) | ||
} | ||
|
||
func search(request: Request, searchQuery: GroupSearchQuery) async throws -> GroupV2 { | ||
let allGroups = try await allGroups(request: request) | ||
let searchedGroup = allGroups.groups.first { | ||
$0.name.lowercased().contains(searchQuery.groupName.lowercased()) | ||
} | ||
if let searchedGroup = searchedGroup { | ||
return searchedGroup | ||
} else { | ||
throw Abort(.notFound, reason: "Group not found") | ||
} | ||
} | ||
|
||
func getLessons(for groupUUID: UUID, request: Request) async throws -> LessonsResponseV2 { | ||
let response: ClientResponse = try await request.client.get( | ||
"https://schedule.kpi.ua/api/schedule/lessons", | ||
beforeSend: { request in | ||
try request.query.encode(["groupId": groupUUID.uuidString]) | ||
} | ||
) | ||
let lessonsV2ClientResponse = try response.content.decode(LessonsV2ClientResponse.self) | ||
return LessonsResponseV2(id: groupUUID, lessons: lessonsV2ClientResponse.lessonsV2()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
Sources/App/Models/RozkladV2/ClientResponse/GroupModelV2ClientResponse.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// GroupModelV2ClientResponse.swift | ||
// | ||
// | ||
// Created by Denys Danyliuk on 02.07.2022. | ||
// | ||
|
||
import Vapor | ||
|
||
struct GroupModelV2ClientResponse: Content { | ||
|
||
var data: [GroupV2] | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
Sources/App/Models/RozkladV2/ClientResponse/LessonsV2ClientResponse.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// LessonsV2ClientResponse.swift | ||
// | ||
// | ||
// Created by Denys Danyliuk on 02.07.2022. | ||
// | ||
|
||
import Vapor | ||
|
||
struct LessonsV2ClientResponse: Content { | ||
|
||
let data: Data | ||
|
||
struct Data: Content { | ||
let scheduleFirstWeek: [Day] | ||
let scheduleSecondWeek: [Day] | ||
} | ||
|
||
struct Day: Content { | ||
let day: String | ||
let pairs: [Pair] | ||
} | ||
|
||
struct Pair: Content { | ||
let teacherName: String | ||
let lecturerId: String | ||
let type: String | ||
let time: String | ||
let name: String | ||
let place: String | ||
let tag: String | ||
} | ||
|
||
} | ||
|
||
extension LessonsV2ClientResponse { | ||
|
||
func lessonsV2() -> [LessonV2] { | ||
let firstWeek = getWeekLessons(from: data.scheduleFirstWeek, week: .first) | ||
let secondWeek = getWeekLessons(from: data.scheduleSecondWeek, week: .second) | ||
return firstWeek + secondWeek | ||
} | ||
|
||
private func getWeekLessons(from days: [LessonsV2ClientResponse.Day], week: LessonV2.Week) -> [LessonV2] { | ||
days | ||
.enumerated() | ||
.flatMap { index, day -> [LessonV2] in | ||
day.pairs | ||
.reduce(into: []) { partialResult, pair in | ||
let firstIndex = partialResult.firstIndex(where: { $0.position.description.contains(pair.time) }) | ||
if let firstIndex = firstIndex { | ||
var old = partialResult.remove(at: firstIndex) | ||
if !old.names.contains(pair.name) { | ||
old.names.append(pair.name) | ||
} | ||
if !(old.teachers?.contains(pair.teacherName) ?? false) { | ||
old.teachers?.append(pair.teacherName) | ||
} | ||
if !(old.locations?.contains(pair.place) ?? false) { | ||
old.locations?.append(pair.place) | ||
} | ||
partialResult.insert(old, at: firstIndex) | ||
|
||
} else { | ||
partialResult.append( | ||
LessonV2( | ||
names: [pair.name], | ||
teachers: [pair.teacherName], | ||
locations: [pair.place], | ||
type: pair.type, | ||
position: .init(pair.time), | ||
day: LessonV2.Day(rawValue: index + 1) ?? .monday, | ||
week: week | ||
) | ||
) | ||
} | ||
} | ||
.sorted { lhs, rhs in | ||
lhs.position.rawValue < rhs.position.rawValue | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// GroupV2.swift | ||
// | ||
// | ||
// Created by Denys Danyliuk on 02.07.2022. | ||
// | ||
|
||
import Vapor | ||
|
||
struct GroupV2: Content { | ||
|
||
var id: UUID? | ||
var name: String | ||
var faculty: String? | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// GroupsResponseV2.swift | ||
// | ||
// | ||
// Created by Denys Danyliuk on 02.07.2022. | ||
// | ||
|
||
import Vapor | ||
|
||
struct GroupsResponseV2: Content { | ||
|
||
let numberOfGroups: Int | ||
let groups: [GroupV2] | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// LessonV2.swift | ||
// | ||
// | ||
// Created by Denys Danyliuk on 02.07.2022. | ||
// | ||
|
||
import Vapor | ||
|
||
public struct LessonV2: Equatable { | ||
|
||
// MARK: - Position | ||
|
||
public enum Position: Int, Codable, CaseIterable, Equatable { | ||
case first = 1 | ||
case second | ||
case third | ||
case fourth | ||
case fifth | ||
case sixth | ||
|
||
init(_ string: String) { | ||
switch string { | ||
case "8.30": | ||
self = .first | ||
|
||
case "10.25": | ||
self = .second | ||
|
||
case "12.20": | ||
self = .third | ||
|
||
case "14.15": | ||
self = .fourth | ||
|
||
case "16.10": | ||
self = .fifth | ||
|
||
case "18.30", "18.05": | ||
self = .sixth | ||
|
||
default: | ||
self = .first | ||
} | ||
} | ||
|
||
var description: [String] { | ||
switch self { | ||
case .first: | ||
return ["8.30"] | ||
|
||
case .second: | ||
return ["10.25"] | ||
|
||
case .third: | ||
return ["12.20"] | ||
|
||
case .fourth: | ||
return ["14.15"] | ||
|
||
case .fifth: | ||
return ["16.10"] | ||
|
||
case .sixth: | ||
return ["18.30", "18.05"] | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Day | ||
|
||
public enum Day: Int, Codable, CaseIterable, Equatable { | ||
case monday = 1 | ||
case tuesday | ||
case wednesday | ||
case thursday | ||
case friday | ||
case saturday | ||
} | ||
|
||
// MARK: - Week | ||
|
||
public enum Week: Int, Codable, Equatable { | ||
case first = 1 | ||
case second | ||
} | ||
|
||
public var names: [String] | ||
public var teachers: [String]? | ||
public var locations: [String]? | ||
public var type: String | ||
|
||
public let position: Position | ||
public let day: Day | ||
public let week: Week | ||
|
||
} | ||
|
||
// MARK: - Codable | ||
|
||
extension LessonV2: Codable { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Denys Danyliuk on 02.07.2022. | ||
// | ||
|
||
import Vapor | ||
|
||
struct LessonsResponseV2 { | ||
var id: UUID | ||
let lessons: [LessonV2] | ||
} | ||
|
||
extension LessonsResponseV2: Codable { | ||
|
||
} | ||
|
||
extension LessonsResponseV2: Content { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.