You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
3.5 KiB
Go
161 lines
3.5 KiB
Go
2 years ago
|
package note
|
||
2 years ago
|
|
||
|
import (
|
||
|
"github.com/ajikamaludin/go-fiber-rest/app/models"
|
||
2 years ago
|
noteRepository "github.com/ajikamaludin/go-fiber-rest/app/repository/note"
|
||
2 years ago
|
gormdb "github.com/ajikamaludin/go-fiber-rest/pkg/gorm.db"
|
||
2 years ago
|
redisclient "github.com/ajikamaludin/go-fiber-rest/pkg/redis.client"
|
||
2 years ago
|
"github.com/ajikamaludin/go-fiber-rest/pkg/utils/constants"
|
||
2 years ago
|
"github.com/ajikamaludin/go-fiber-rest/pkg/utils/validator"
|
||
|
"github.com/gofiber/fiber/v2"
|
||
|
)
|
||
|
|
||
|
func GetAllNotes(c *fiber.Ctx) error {
|
||
2 years ago
|
var notes []models.Note
|
||
|
|
||
2 years ago
|
err := noteRepository.GetAllNotes(¬es)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||
|
"status": constants.STATUS_FAIL,
|
||
|
"message": "Internal Service Error",
|
||
|
"error": err.Error(),
|
||
|
})
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||
|
"status": constants.STATUS_SUCCESS,
|
||
|
"message": "Ok",
|
||
|
"data": notes,
|
||
|
})
|
||
2 years ago
|
}
|
||
|
|
||
|
func GetNoteById(c *fiber.Ctx) error {
|
||
|
id := c.Params("id")
|
||
2 years ago
|
note := models.Note{}
|
||
2 years ago
|
|
||
2 years ago
|
err := noteRepository.GetNoteById(id, ¬e)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||
|
"status": constants.STATUS_FAIL,
|
||
|
"message": "note not found",
|
||
|
"error": err.Error(),
|
||
|
})
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||
|
"status": constants.STATUS_OK,
|
||
|
"message": "note found",
|
||
|
"data": note,
|
||
|
})
|
||
2 years ago
|
}
|
||
|
|
||
|
func CreateNote(c *fiber.Ctx) error {
|
||
|
noteRequest := new(models.Note)
|
||
|
|
||
|
if err := c.BodyParser(¬eRequest); err != nil {
|
||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||
|
"message": err.Error(),
|
||
|
})
|
||
|
}
|
||
|
|
||
|
errors := validator.ValidateRequest(noteRequest)
|
||
|
if errors != nil {
|
||
|
return c.Status(fiber.StatusUnprocessableEntity).JSON(errors)
|
||
|
}
|
||
|
|
||
|
db, err := gormdb.GetInstance()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
var note = models.Note{
|
||
|
User_id: "1",
|
||
|
Title: noteRequest.Note,
|
||
|
Note: noteRequest.Title,
|
||
|
}
|
||
|
|
||
|
db.Create(¬e)
|
||
|
|
||
2 years ago
|
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
|
||
|
"status": constants.STATUS_OK,
|
||
|
"message": "note created",
|
||
|
"data": note,
|
||
|
})
|
||
2 years ago
|
}
|
||
|
|
||
|
func UpdateNote(c *fiber.Ctx) error {
|
||
|
// validate request first
|
||
|
noteRequest := new(models.Note)
|
||
|
|
||
|
if err := c.BodyParser(¬eRequest); err != nil {
|
||
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||
2 years ago
|
"status": constants.STATUS_FAIL,
|
||
2 years ago
|
"message": err.Error(),
|
||
|
})
|
||
|
}
|
||
|
|
||
|
errors := validator.ValidateRequest(noteRequest)
|
||
|
if errors != nil {
|
||
2 years ago
|
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
|
||
|
"status": constants.STATUS_FAIL,
|
||
|
"message": "error",
|
||
|
"errors": errors,
|
||
|
})
|
||
2 years ago
|
}
|
||
|
|
||
|
// find records
|
||
|
id := c.Params("id")
|
||
|
|
||
|
db, err := gormdb.GetInstance()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
note := models.Note{}
|
||
|
err = db.First(¬e, id).Error
|
||
|
|
||
|
if err != nil {
|
||
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
||
2 years ago
|
"status": constants.STATUS_FAIL,
|
||
2 years ago
|
"message": "note not found",
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// Update
|
||
|
db.Model(¬e).Updates(noteRequest)
|
||
2 years ago
|
key := "note+" + id
|
||
|
redisclient.Remove(key)
|
||
2 years ago
|
|
||
2 years ago
|
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
|
||
|
"status": constants.STATUS_OK,
|
||
|
"message": "note updated",
|
||
|
"data": note,
|
||
|
})
|
||
2 years ago
|
}
|
||
|
|
||
|
func DeleteNote(c *fiber.Ctx) error {
|
||
|
// find records
|
||
|
id := c.Params("id")
|
||
|
|
||
|
db, err := gormdb.GetInstance()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
note := models.Note{}
|
||
|
err = db.First(¬e, id).Error
|
||
|
|
||
|
if err != nil {
|
||
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
||
2 years ago
|
"status": constants.STATUS_FAIL,
|
||
2 years ago
|
"message": "note not found",
|
||
|
})
|
||
|
}
|
||
|
|
||
|
db.Delete(¬e)
|
||
2 years ago
|
key := "note+" + id
|
||
|
redisclient.Remove(key)
|
||
2 years ago
|
|
||
|
return c.SendStatus(fiber.StatusNoContent)
|
||
|
}
|