From d4d51f01c452dcf5e9402da84a5582d8573696ab Mon Sep 17 00:00:00 2001 From: Austin Lim Date: Wed, 13 Nov 2024 12:29:40 -0800 Subject: [PATCH 1/4] deleteing comments seems to work --- .../DataLayer/Utils/DataUtils.swift | 16 ++++++++++ .../ViewLayer/commentView.swift | 15 +++++++--- .../DataUtilsTesting.swift | 29 +++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift index 74066ac..4b11a60 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift @@ -124,5 +124,21 @@ struct DataUtils { } + + static func addComment(for context: ModelContext, comment: String)throws{ + + } + + static func deleteComment(for context: ModelContext, comment: Comment)throws { + context.delete(comment) + + do{ + try context.save() + } + catch{ + print("Error: Comment could not be delted from database") + throw DataError.couldNotSave + } + } } diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift index 86c6399..d3a91c0 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift @@ -15,6 +15,8 @@ struct commentView: View{ let isAdmin: Bool let isLoggedIn: Bool + @Environment(\.modelContext) var context + var body : some View{ VStack{ List(comments, id: \.createdAt) { comment in @@ -27,7 +29,7 @@ struct commentView: View{ Text(comment.contents) .font(.body) } - if isAdmin { + if isAdmin && isLoggedIn { Button(action: { commentToDelete = comment showDeleteConfirmation = true @@ -47,7 +49,7 @@ struct commentView: View{ } .navigationTitle("Comments") - if isLoggedIn || isAdmin { + if isLoggedIn && isAdmin { NavigationLink(destination: newCommentView()) { Text("Add a comment") .font(.caption) @@ -69,7 +71,12 @@ struct commentView: View{ } } func deleteComment(_ comment: Comment) { - comments.removeAll { $0.createdAt == comment.createdAt } - print("Comment deleted: \(comment.contents)") + do{ + try DataUtils.deleteComment(for: context, comment: comment) + comments.removeAll { $0.createdAt == comment.createdAt } + print("Comment deleted: \(comment.contents)") + } catch{ + print("Failed to delete comment: \(error)") + } } } diff --git a/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift b/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift index bd75ad9..17cf077 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift @@ -89,5 +89,34 @@ class DataUtilsTesting { let result = try! DataUtils.userLogin(for: notLoggedUser, context: modelContext) #expect(result == true) } + + @Test("delete a comment") func deleteComment() { + let context = self.modelContext + var commentsInContext: [miniProject_seniorDesign.Comment] = [] + + let user = User(name: "Test User", email: "testuser@example.com", admin: false) + context.insert(user) + + let post = Post(user: user, contents: "Test post content") + context.insert(post) + + + let comment = Comment(user: user, post: post, contents: "Test comment", createdAt: Date()) + context.insert(comment) + commentsInContext.append(comment) + + #expect(commentsInContext.contains { $0 === comment } == true) + + // Test delete + do { + try DataUtils.deleteComment(for: context, comment: comment) + commentsInContext.removeAll { $0 === comment } + let isCommentDeleted = !commentsInContext.contains { $0 === comment } + #expect(isCommentDeleted == true) + } catch { + #expect(Bool(false), "Error deleting comment: \(error)") + } + } + } From ab08535581046b246e86652805fb6b8d67ef0c78 Mon Sep 17 00:00:00 2001 From: Austin Lim Date: Wed, 13 Nov 2024 17:35:29 -0800 Subject: [PATCH 2/4] add comments button almost working --- .../DataLayer/Utils/DataUtils.swift | 11 ++++++- .../ViewLayer/ContentView.swift | 2 +- .../ViewLayer/commentView.swift | 6 +++- .../ViewLayer/newCommentView.swift | 29 ++++++++++++++++++- .../DataUtilsTesting.swift | 3 +- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift index 4b11a60..d41c979 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift @@ -125,7 +125,16 @@ struct DataUtils { } - static func addComment(for context: ModelContext, comment: String)throws{ + static func addComment(for context: ModelContext, post: Post, user: User, contents: String)throws -> Comment{ + let comment = Comment(user: user, post:post, contents: contents, createdAt: Date()) + context.insert(comment) + do{ + try context.save() + return comment + } catch{ + print("Error: Comment could not be saved to the database") + throw DataError.couldNotSave + } } diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift index 2ffe1ae..9666590 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/ContentView.swift @@ -87,7 +87,7 @@ struct ContentView: View { } - NavigationLink(destination: commentView(comments: post.comments, isAdmin: isAdmin, isLoggedIn: isLoggedIn)) { + NavigationLink(destination: commentView(comments: post.comments, isAdmin: isAdmin, isLoggedIn: isLoggedIn, selectedPost: post, currentUser: post.user)) { Text("View all comments") .font(.caption) .foregroundColor(.blue) diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift index d3a91c0..11bcb9e 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift @@ -14,12 +14,16 @@ struct commentView: View{ @State var comments: [Comment] let isAdmin: Bool let isLoggedIn: Bool + let selectedPost: Post + let currentUser: User + @Environment(\.modelContext) var context var body : some View{ VStack{ List(comments, id: \.createdAt) { comment in + Text(comment.contents) @@ -50,7 +54,7 @@ struct commentView: View{ .navigationTitle("Comments") if isLoggedIn && isAdmin { - NavigationLink(destination: newCommentView()) { + NavigationLink(destination: newCommentView(selectedPost: selectedPost, currentUser: currentUser, comments: $comments)) { Text("Add a comment") .font(.caption) .foregroundColor(.blue) diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift index a3f87db..1f3fd6e 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift @@ -7,19 +7,46 @@ import SwiftUI struct newCommentView: View{ - @State var newCommentString = "" + @State private var newCommentString = "" + @Environment(\.modelContext) var context + + let selectedPost: Post + let currentUser: User + @Binding var comments: [Comment] + var body: some View{ VStack{ Text("Add a comment").font(.title2) TextField("Enter a comment", text: $newCommentString) .textFieldStyle(RoundedBorderTextFieldStyle()) .padding(.horizontal, 20) + Button(action: { + addComment() + }){ + Text("Post") + .padding() + .background(Color.blue) + .foregroundColor(.white) + .cornerRadius(8) + } + .padding(.top, 10) + + } + } + func addComment(){ + do{ + let comment = try DataUtils.addComment(for: context, post: selectedPost, user: currentUser, contents: newCommentString) + newCommentString = "" + comments.append(comment) + }catch{ + print("Failed to add post: \(error)") } } } + #Preview { ContentView() } diff --git a/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift b/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift index 17cf077..6f529a6 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift @@ -94,7 +94,7 @@ class DataUtilsTesting { let context = self.modelContext var commentsInContext: [miniProject_seniorDesign.Comment] = [] - let user = User(name: "Test User", email: "testuser@example.com", admin: false) + let user = User(name: "Test User", email: "testuser@example.com", admin: true) context.insert(user) let post = Post(user: user, contents: "Test post content") @@ -107,7 +107,6 @@ class DataUtilsTesting { #expect(commentsInContext.contains { $0 === comment } == true) - // Test delete do { try DataUtils.deleteComment(for: context, comment: comment) commentsInContext.removeAll { $0 === comment } From 621f72786cbed8c17ec7f2504cce06b32b5a8828 Mon Sep 17 00:00:00 2001 From: Austin Lim Date: Wed, 13 Nov 2024 18:00:23 -0800 Subject: [PATCH 3/4] fully working add comment and delete comment --- .../Preview Content/ViewLayer/commentView.swift | 4 ---- .../Preview Content/ViewLayer/newCommentView.swift | 1 + 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift index 11bcb9e..1b650bd 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/commentView.swift @@ -23,10 +23,6 @@ struct commentView: View{ var body : some View{ VStack{ List(comments, id: \.createdAt) { comment in - Text(comment.contents) - - - VStack(alignment: .leading) { HStack{ VStack{ diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift index 1f3fd6e..386bd89 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift @@ -38,6 +38,7 @@ struct newCommentView: View{ let comment = try DataUtils.addComment(for: context, post: selectedPost, user: currentUser, contents: newCommentString) newCommentString = "" comments.append(comment) + }catch{ print("Failed to add post: \(error)") } From f7be0c1cd915c11a9bfecf5432c74f94b0d0af49 Mon Sep 17 00:00:00 2001 From: Austin Lim Date: Wed, 13 Nov 2024 18:34:15 -0800 Subject: [PATCH 4/4] fully functioning add comments and delete comments wiht working tests --- .../DataLayer/Utils/DataUtils.swift | 1 + .../ViewLayer/newCommentView.swift | 1 + .../DataUtilsTesting.swift | 41 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift index d41c979..8bf9661 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/DataLayer/Utils/DataUtils.swift @@ -126,6 +126,7 @@ struct DataUtils { } static func addComment(for context: ModelContext, post: Post, user: User, contents: String)throws -> Comment{ + guard !contents.isEmpty else { throw DataError.invalidContent } let comment = Comment(user: user, post:post, contents: contents, createdAt: Date()) context.insert(comment) do{ diff --git a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift index 386bd89..50d6de0 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesign/Preview Content/ViewLayer/newCommentView.swift @@ -39,6 +39,7 @@ struct newCommentView: View{ newCommentString = "" comments.append(comment) + }catch{ print("Failed to add post: \(error)") } diff --git a/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift b/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift index 6f529a6..5afa10e 100644 --- a/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift +++ b/mini-project-SeniorDesign/miniProject-seniorDesignTests/DataUtilsTesting.swift @@ -116,6 +116,47 @@ class DataUtilsTesting { #expect(Bool(false), "Error deleting comment: \(error)") } } + @Test("add a empty comment")func addEmptyComment(){ + let context = self.modelContext + var commentsInContext: [miniProject_seniorDesign.Comment] = [] + + let user = User(name: "Test User", email: "testuser@example.com", admin: true) + context.insert(user) + + let post = Post(user: user, contents: "Test post content") + context.insert(post) + + let contents = "" + + do{ + let comment = try DataUtils.addComment(for: context, post: post, user: user, contents: contents) + commentsInContext.append(comment) + #expect(Bool(false), "Expected an error when adding an empty comment, but it succeeded.") + + }catch{ + #expect(commentsInContext.isEmpty == true, "No comment should be added for empty content") + } + } + @Test("add a comment")func addComment(){ + let context = self.modelContext + var commentsInContext: [miniProject_seniorDesign.Comment] = [] + + let user = User(name: "Test User", email: "testuser@example.com", admin: true) + context.insert(user) + + let post = Post(user: user, contents: "Test post content") + context.insert(post) + + let contents = "This is a test comment" + do{ + let comment = try DataUtils.addComment(for: context, post: post, user: user, contents: contents) + commentsInContext.append(comment) + #expect(!commentsInContext.isEmpty == true) + + }catch{ + #expect(Bool(false), "Error adding a valid comment: \(error)") + } + } }