projects.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package handlers
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. "notabug.org/alimiracle/my-website-api/models"
  5. "notabug.org/alimiracle/my-website-api/responses"
  6. )
  7. func (self Handler) GetAllProjects(c *fiber.Ctx) error {
  8. var projects []models.Project
  9. self.Database.FindAll(&projects)
  10. return c.Status(fiber.StatusOK).JSON(&projects)
  11. }
  12. func (self Handler) AddProject(c *fiber.Ctx) error {
  13. body := models.Project{}
  14. if err := c.BodyParser(&body); err != nil {
  15. return responses.BadRequestResponse(c, err)
  16. }
  17. // insert new db entry
  18. err := self.Database.Create(&body)
  19. return responses.Created_Response(c, &body, err)
  20. }
  21. func (self Handler) UpdateProject(c *fiber.Ctx) error {
  22. id := c.Params("id")
  23. body := models.Project{}
  24. if err := c.BodyParser(&body); err != nil {
  25. return responses.BadRequestResponse(c, err)
  26. }
  27. err := self.Database.Update(&body, id)
  28. return responses.Response(c, err)
  29. }
  30. func (self Handler) DeleteProject(c *fiber.Ctx) error {
  31. id := c.Params("id")
  32. var project models.Project
  33. err := self.Database.Delete(&project, id)
  34. return responses.Response(c, err)
  35. }