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

Revert "Austins special branch" #49

Merged
merged 1 commit into from
Nov 14, 2024
Merged
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
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)")
}
}

}