Skip to content

Commit

Permalink
Revert "Austins special branch"
Browse files Browse the repository at this point in the history
  • Loading branch information
austinlim31 authored Nov 14, 2024
1 parent e6a8ea0 commit 8e8fc4f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,32 @@ 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, selectedPost: post, currentUser: post.user)) {
NavigationLink(destination: commentView(comments: post.comments, isAdmin: isAdmin, isLoggedIn: isLoggedIn)) {
Text("View all comments")
.font(.caption)
.foregroundColor(.blue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ 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 && isLoggedIn {
if isAdmin {
Button(action: {
commentToDelete = comment
showDeleteConfirmation = true
Expand All @@ -49,8 +47,8 @@ struct commentView: View{
}
.navigationTitle("Comments")

if isLoggedIn && isAdmin {
NavigationLink(destination: newCommentView(selectedPost: selectedPost, currentUser: currentUser, comments: $comments)) {
if isLoggedIn || isAdmin {
NavigationLink(destination: newCommentView()) {
Text("Add a comment")
.font(.caption)
.foregroundColor(.blue)
Expand All @@ -71,12 +69,7 @@ struct commentView: View{
}
}
func deleteComment(_ comment: Comment) {
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)")
}
comments.removeAll { $0.createdAt == comment.createdAt }
print("Comment deleted: \(comment.contents)")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,19 @@
import SwiftUI

struct newCommentView: View{
@State private var newCommentString = ""
@Environment(\.modelContext) var context

let selectedPost: Post
let currentUser: User
@Binding var comments: [Comment]

@State var newCommentString = ""
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,74 +89,5 @@ 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 8e8fc4f

Please sign in to comment.