Skip to content

Commit

Permalink
Merge pull request #48 from usd-cs/AustinsSpecialBranch
Browse files Browse the repository at this point in the history
Austins special branch
  • Loading branch information
daranda787 authored Nov 14, 2024
2 parents e4f003d + a493d15 commit e6a8ea0
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,32 +129,6 @@ func addPost(for commenting_user: User, post_content: String, logged_users: [Use


}
func printAllUsers(context: ModelContext) {
let fetchRequest = FetchDescriptor<User>() // Create a fetch descriptor for User
//print(fetchRequest)
do {
let users: [User] = try context.fetch(fetchRequest) // Fetch all users
print("Users in Database:")
for user in users {
print("Name: \(user.name), Email: \(user.email), Admin: \(user.admin)")
}
} catch {
print("Failed to fetch users: \(error.localizedDescription)")
}
}

func printAllPosts(context: ModelContext) {
let fetchRequest = FetchDescriptor<Post>() // Create a fetch descriptor for Post
do {
let posts: [Post] = try context.fetch(fetchRequest) // Fetch all posts
print("Posts in Database:")
for post in posts {
print("Contents: \(post.contents), Created At: \(post.createdAt), Posted By: \(post.user.name)")
}
} catch {
print("Failed to fetch posts: \(error.localizedDescription)")
}
}

func printAllComments(context: ModelContext) {
let fetchRequest = FetchDescriptor<Comment>() // Create a fetch descriptor for Comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ 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



VStack(alignment: .leading) {
HStack{
VStack{
Text(comment.contents)
.font(.body)
}
if isAdmin {
if isAdmin && isLoggedIn {
Button(action: {
commentToDelete = comment
showDeleteConfirmation = true
Expand All @@ -47,8 +49,8 @@ struct commentView: View{
}
.navigationTitle("Comments")

if isLoggedIn || isAdmin {
NavigationLink(destination: newCommentView()) {
if isLoggedIn && isAdmin {
NavigationLink(destination: newCommentView(selectedPost: selectedPost, currentUser: currentUser, comments: $comments)) {
Text("Add a comment")
.font(.caption)
.foregroundColor(.blue)
Expand All @@ -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)")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,48 @@
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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,74 @@ 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: "[email protected]", admin: true)
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)

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)")
}
}
@Test("add a empty comment")func addEmptyComment(){
let context = self.modelContext
var commentsInContext: [miniProject_seniorDesign.Comment] = []

let user = User(name: "Test User", email: "[email protected]", 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: "[email protected]", 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)")
}
}

}

0 comments on commit e6a8ea0

Please sign in to comment.