Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified Announcements class to take in formatted and raw text #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ let package = Package(
.package(
url: "https://github.com/vincentneo/CoreGPX.git",
from: "0.9.2"
),
.package(
url: "https://github.com/apple/swift-markdown.git",
branch: "main"
)
],
targets: [
Expand Down Expand Up @@ -108,6 +112,10 @@ let package = Package(
.product(
name: "Turf",
package: "turf-swift"
),
.product(
name: "Markdown",
package: "swift-markdown"
)
]
),
Expand Down
11 changes: 11 additions & 0 deletions Sources/App/MarkDownTextExtractor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Markdown

struct MarkdownTextExtractor: MarkupWalker {

private(set) var rawText = ""

mutating func visitText(_ text: Text) {
rawText.append(text.plainText)
}

}
22 changes: 21 additions & 1 deletion Sources/App/Models/Announcement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,27 @@ final class Announcement: VersionedModel, Content {
}
}

init() { }
// Announcement object initializer
init{} (
id: UUID?,
subject: String,
body: String,
start: String,
end: String,
scheduleType: ScheduleType,
interruptionLevel: InterruptionLevel,
signature: Data
) {
self.id = id
self.subject = subject
self.body = body
self.start = start
self.end = end
self.scheduleType = scheduleType
self.interruptionLevel = interruptionLevel
self.signature = signature
}


}

Expand Down
40 changes: 33 additions & 7 deletions Sources/App/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Algorithms
import APNSCore
import Fluent
import Markdown
import UAParserSwift
import Vapor
import VaporAPNS
Expand Down Expand Up @@ -140,13 +141,38 @@ func routes(_ application: Application) throws {
}
}

// Get the current announcements
application.get("announcements") { (request) in
return try await Announcement
.query(on: request.db(.psql))
.all()
}

// Get the current announcements with formatting
application.get("announcements", "formatted") { (request) in
return try await Announcement
.query(on: request.db(.psql))
.all()
}

//Get the current announcements in raw text format
application.get("announcements") { (request) in
let announcementList = try await Announcement
.query(on: request db(.psql))
.all()
//strips markdown formatting from each announcement
for oldAnnouncement in announcementList {
let document = Document(parsing: oldAnnouncement.body)
//print(document.debugDescription())
var extractor = MarkdownTextExtractor()
extractor.visit(document)
//print(extractor.rawText)
var newAnnouncement = Announcement(
oldAnnouncement.id,
oldAnnouncement.subject,
extractor.rawText,
oldAnnouncement.start
oldAnnouncement.end,
oldAnnouncement.scheduleType,
oldAnnouncement.interruptionLevel,
oldAnnouncement.signature
)
}
}

// Post a new announcement after verifying the request
application.post("announcements") { (request) -> Announcement in
let announcement = try request.content.decode(Announcement.self, using: decoder)
Expand Down