generated from GDGVIT/template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: users can save course-wise notes
* fix: merged slot for lab while fetching timetable * feat: users can save course-wise notes --------- Co-authored-by: Dev Keshwani <[email protected]>
- Loading branch information
1 parent
4d87c65
commit 6d2e84c
Showing
8 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package serializers | ||
|
||
import "github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/models" | ||
|
||
func NotesSerializer(notes []models.Notes) []map[string]interface{} { | ||
var result []map[string]interface{} | ||
|
||
for _, note := range notes { | ||
out := map[string]interface{}{ | ||
"note_id": note.NoteID, | ||
"note_name": note.NoteName, | ||
"note_content": note.NoteContent, | ||
"course_id": note.CourseID, | ||
"course_name": note.Courses.CourseName, | ||
} | ||
result = append(result, out) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package v2 | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/GDGVIT/vitty-backend/vitty-backend-api/api/middleware" | ||
"github.com/GDGVIT/vitty-backend/vitty-backend-api/api/serializers" | ||
"github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/models" | ||
"github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/utils" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func noteHandler(api fiber.Router) { | ||
group := api.Group("/notes") | ||
group.Use(middleware.JWTAuthMiddleware) | ||
group.Get("/:courseId", getNotes) | ||
group.Post("/save", saveNote) | ||
group.Delete("/:noteId?", deleteNote) | ||
} | ||
|
||
func getNotes(c *fiber.Ctx) error { | ||
var notes models.Notes | ||
courseId := c.Params("courseId") | ||
notes.CourseID = courseId | ||
notes.UserName = c.Locals("user").(models.User).Username | ||
err, userNotes := notes.GetNotesByCourseId() | ||
if err != nil { | ||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ | ||
"error": "Notes fetch failed", | ||
}) | ||
} | ||
|
||
if len(userNotes) == 0 { | ||
return c.Status(fiber.StatusOK).JSON(fiber.Map{ | ||
"data": "No notes to display", | ||
}) | ||
} | ||
|
||
return c.Status(fiber.StatusOK).JSON(fiber.Map{ | ||
"data": serializers.NotesSerializer(userNotes), | ||
}) | ||
} | ||
|
||
func saveNote(c *fiber.Ctx) error { | ||
var note models.Notes | ||
|
||
if err := c.BodyParser(¬e); err != nil { | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"error": "Invalid request body", | ||
}) | ||
} | ||
|
||
if !strings.Contains(note.NoteID, "note_") || len(note.NoteID) < 32 { | ||
note.NoteID = utils.UUIDWithPrefix("note") | ||
} | ||
|
||
note.SaveNote() | ||
|
||
return c.Status(fiber.StatusOK).JSON(fiber.Map{ | ||
"detail": "Note Saved Successfully", | ||
}) | ||
} | ||
|
||
func deleteNote(c *fiber.Ctx) error { | ||
var note models.Notes | ||
|
||
noteId := c.Params("noteId") | ||
if noteId == "" { | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"detail": "Note id is missing", | ||
}) | ||
} | ||
|
||
note.NoteID = noteId | ||
note.UserName = c.Locals("user").(models.User).Username | ||
|
||
err := note.DeleteNote() | ||
|
||
if err != nil { | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.ErrBadRequest) | ||
} | ||
return c.Status(fiber.StatusOK).JSON(fiber.Map{ | ||
"detail": "Note deleted successfully", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package models | ||
|
||
import ( | ||
"github.com/GDGVIT/vitty-backend/vitty-backend-api/internal/database" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Notes struct { | ||
NoteID string `json:"note_id,omitempty" gorm:"unique"` | ||
NoteName string `json:"note_name" gorm:"primaryKey"` | ||
UserName string `json:"user_name" gorm:"primaryKey"` | ||
CourseID string `json:"course_id"` | ||
NoteContent string `json:"note_content"` | ||
User User `gorm:"foreignKey:UserName;references:Username;constraint:OnDelete:CASCADE"` | ||
Courses Courses ` gorm:"foreignKey:CourseID;references:CourseId;constraint:OnDelete:CASCADE"` | ||
} | ||
|
||
func (n *Notes) SaveNote() error { | ||
result := database.DB.Save(n) | ||
|
||
if result.RowsAffected == 0 { | ||
return gorm.ErrRecordNotFound | ||
} | ||
|
||
return result.Error | ||
} | ||
|
||
func (n *Notes) DeleteNote() error { | ||
result := database.DB.Where("user_name = ?", n.UserName).Delete(&n) | ||
if result.RowsAffected == 0 { | ||
return gorm.ErrRecordNotFound | ||
} | ||
|
||
return result.Error | ||
} | ||
|
||
func (n *Notes) GetNotesByCourseId() (error, []Notes) { | ||
var notes []Notes | ||
err := database.DB.Where(n).Preload("Courses").Find(¬es).Error | ||
return err, notes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters