Skip to content

Commit 735c360

Browse files
committed
fix: resolve submission retrieval issues and add admin comment functionality
- Add case-insensitive username matching in get_user_submissions handler - Allow task resubmissions when not verified - Ensure both 'user' and 'username' fields are populated - Add comprehensive debug logging
1 parent 5f3415d commit 735c360

28 files changed

+267
-74
lines changed

database/connect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package database
22

33
import (
4-
"CA_Backend/config"
4+
"CA_Portal_backend/config"
55
"context"
66
"fmt"
77
"go.mongodb.org/mongo-driver/mongo"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module CA_Backend
1+
module CA_Portal_backend
22

33
go 1.22.2
44

handler/leaderboard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package handler
22

33
import (
4-
"CA_Backend/database"
4+
"CA_Portal_backend/database"
55
"context"
66
"github.com/gofiber/fiber/v2"
77
"go.mongodb.org/mongo-driver/bson"

handler/tasks/add_admin_comment.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package tasks
2+
3+
import (
4+
"CA_Portal_backend/database"
5+
"CA_Portal_backend/models"
6+
"context"
7+
8+
// "time"
9+
10+
"github.com/gofiber/fiber/v2"
11+
"go.mongodb.org/mongo-driver/bson"
12+
"go.mongodb.org/mongo-driver/bson/primitive"
13+
)
14+
15+
// POST /api/submissions/comment/:submission_id
16+
func AddAdminComment(c *fiber.Ctx) error {
17+
ctx := context.Background()
18+
19+
// Get submission ID from params
20+
submissionID := c.Params("submission_id")
21+
objectID, err := primitive.ObjectIDFromHex(submissionID)
22+
if err != nil {
23+
return c.Status(400).JSON(fiber.Map{
24+
"message": "Invalid submission ID",
25+
})
26+
}
27+
28+
// Parse request body
29+
var req struct {
30+
Comment string `json:"comment" binding:"required"`
31+
}
32+
33+
if err := c.BodyParser(&req); err != nil {
34+
return c.Status(400).JSON(fiber.Map{
35+
"error": err.Error(),
36+
"message": "Failed to parse JSON body",
37+
})
38+
}
39+
40+
if req.Comment == "" {
41+
return c.Status(400).JSON(fiber.Map{
42+
"message": "Comment cannot be empty",
43+
})
44+
}
45+
46+
// Connect to database
47+
db, err := database.Connect()
48+
if err != nil {
49+
return c.Status(500).JSON(fiber.Map{
50+
"message": "Database connection error: " + err.Error(),
51+
})
52+
}
53+
54+
collection := db.Collection("task_submissions")
55+
56+
// Check if submission exists
57+
var submission models.TaskSubmission
58+
err = collection.FindOne(ctx, bson.M{"_id": objectID}).Decode(&submission)
59+
if err != nil {
60+
return c.Status(404).JSON(fiber.Map{
61+
"message": "Submission not found",
62+
})
63+
}
64+
65+
// Update submission with new comment
66+
update := bson.M{
67+
"$set": bson.M{
68+
"admin_comment": req.Comment,
69+
"verified": false, // Reset verification when admin comments
70+
},
71+
}
72+
73+
result, err := collection.UpdateOne(ctx, bson.M{"_id": objectID}, update)
74+
if err != nil {
75+
return c.Status(500).JSON(fiber.Map{
76+
"message": "Failed to add comment: " + err.Error(),
77+
})
78+
}
79+
80+
if result.MatchedCount == 0 {
81+
return c.Status(404).JSON(fiber.Map{
82+
"message": "Submission not found",
83+
})
84+
}
85+
86+
return c.Status(200).JSON(fiber.Map{
87+
"message": "Comment added successfully",
88+
"submission_id": submissionID,
89+
"comment": req.Comment,
90+
})
91+
}

handler/tasks/create_task.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package tasks
22

33
import (
4-
"CA_Backend/database"
5-
"CA_Backend/models"
4+
"CA_Portal_backend/database"
5+
"CA_Portal_backend/models"
66
"context"
7-
"github.com/gofiber/fiber/v2"
87
"time"
8+
9+
"github.com/gofiber/fiber/v2"
910
)
1011

1112
func CreateTask(c *fiber.Ctx) error {

handler/tasks/delete_task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package tasks
22

33
import (
4-
"CA_Backend/database"
4+
"CA_Portal_backend/database"
55
"context"
66

77
"github.com/gofiber/fiber/v2"

handler/tasks/get_all_submissions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package tasks
22

33
import (
4-
"CA_Backend/database"
5-
"CA_Backend/models"
4+
"CA_Portal_backend/database"
5+
"CA_Portal_backend/models"
66
"context"
77
"log"
88

handler/tasks/get_all_tasks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package tasks
22

33
import (
4-
"CA_Backend/database"
5-
"CA_Backend/models"
4+
"CA_Portal_backend/database"
5+
"CA_Portal_backend/models"
66
"context"
77
"log"
88

handler/tasks/get_task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package tasks
22

33
import (
4-
"CA_Backend/database"
5-
"CA_Backend/models"
4+
"CA_Portal_backend/database"
5+
"CA_Portal_backend/models"
66
"context"
77
"log"
88

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,62 @@
11
package tasks
22

33
import (
4-
"CA_Backend/database"
5-
"CA_Backend/models"
6-
"CA_Backend/utils"
4+
"CA_Portal_backend/database"
5+
"CA_Portal_backend/models"
6+
"CA_Portal_backend/utils"
77
"context"
88
"log"
9+
"strings"
910

1011
"github.com/gofiber/fiber/v2"
1112
"go.mongodb.org/mongo-driver/bson"
1213
"go.mongodb.org/mongo-driver/mongo/options"
1314
)
1415

1516
func GetUserSubmissions(c *fiber.Ctx) error {
17+
log.Printf("=== GET USER SUBMISSIONS REQUEST ===")
18+
1619
ctx := context.Background()
1720
db, err := database.Connect()
1821
if err != nil {
19-
log.Fatal(err.Error())
22+
log.Printf("ERROR: Database connection failed: %v", err)
2023
return c.Status(500).JSON(fiber.Map{
2124
"error": err.Error(),
2225
"message": "Database connection error",
2326
})
2427
}
28+
log.Printf("✓ Database connected: %s", db.Name())
2529

2630
tokenString := c.Get("Authorization")
31+
log.Printf("Authorization header: %s", tokenString[:50]+"...") // Log first 50 chars
32+
2733
if len(tokenString) < 8 || tokenString[:7] != "Bearer " {
34+
log.Printf("ERROR: Invalid authorization header format")
2835
return c.Status(400).JSON(fiber.Map{"message": "Authorization header missing or improperly formatted"})
2936
}
3037
token := tokenString[7:]
3138

32-
username, _ := utils.DeserialiseUser(token)
39+
username, err := utils.DeserialiseUser(token)
40+
if err != nil {
41+
log.Printf("ERROR: Failed to deserialize token: %v", err)
42+
return c.Status(401).JSON(fiber.Map{"message": "Invalid token"})
43+
}
44+
45+
log.Printf("Username from token: '%s'", username)
46+
47+
// Convert to lowercase for consistent querying
48+
usernameLower := strings.ToLower(username)
49+
log.Printf("Username (lowercase): '%s'", usernameLower)
3350

3451
var user_submissions []models.TaskSubmission
3552

36-
cursor, err := db.Collection("task_submissions").Find(ctx, bson.D{{Key: "User", Value: username}}, options.Find())
53+
// Query using lowercase username
54+
query := bson.M{"user": usernameLower}
55+
log.Printf("MongoDB query: %+v", query)
56+
57+
cursor, err := db.Collection("task_submissions").Find(ctx, query, options.Find())
3758
if err != nil {
59+
log.Printf("ERROR: Database query failed: %v", err)
3860
return c.Status(500).JSON(fiber.Map{
3961
"error": err.Error(),
4062
"message": "Failed to fetch submissions",
@@ -43,11 +65,35 @@ func GetUserSubmissions(c *fiber.Ctx) error {
4365
defer cursor.Close(ctx)
4466

4567
if err := cursor.All(ctx, &user_submissions); err != nil {
68+
log.Printf("ERROR: Failed to decode results: %v", err)
4669
return c.Status(500).JSON(fiber.Map{
4770
"error": err.Error(),
4871
"message": "Error decoding submissions",
4972
})
5073
}
5174

75+
log.Printf("Query result: Found %d submissions", len(user_submissions))
76+
77+
if len(user_submissions) > 0 {
78+
for i, sub := range user_submissions {
79+
log.Printf(" [%d] ID: %s, Task: %s, User: %s, Username: %s, AdminComment: '%s'",
80+
i+1, sub.ID.Hex(), sub.Task, sub.User, sub.Username, sub.AdminComment)
81+
}
82+
} else {
83+
log.Printf(" ⚠️ No submissions found for user: %s", usernameLower)
84+
85+
// Debug: Check if any submissions exist
86+
count, _ := db.Collection("task_submissions").CountDocuments(ctx, bson.M{})
87+
log.Printf(" Total submissions in collection: %d", count)
88+
}
89+
90+
// Return empty array instead of nil
91+
if user_submissions == nil {
92+
user_submissions = []models.TaskSubmission{}
93+
}
94+
95+
log.Printf("Returning JSON with %d submissions", len(user_submissions))
96+
log.Printf("===================================\n")
97+
5298
return c.Status(200).JSON(user_submissions)
53-
}
99+
}

0 commit comments

Comments
 (0)