controller.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package authenticaor
  2. import (
  3. model "github.com/ente-io/museum/ente/authenticator"
  4. "github.com/ente-io/museum/pkg/repo/authenticator"
  5. "github.com/ente-io/museum/pkg/utils/auth"
  6. "github.com/ente-io/stacktrace"
  7. "github.com/google/uuid"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // Controller is interface for exposing business logic related to authenticator app
  11. type Controller struct {
  12. Repo *authenticator.Repository
  13. }
  14. // CreateKey...
  15. func (c *Controller) CreateKey(ctx *gin.Context, req model.CreateKeyRequest) error {
  16. userID := auth.GetUserID(ctx.Request.Header)
  17. return c.Repo.CreateKey(ctx, userID, req)
  18. }
  19. // GetKey...
  20. func (c *Controller) GetKey(ctx *gin.Context) (*model.Key, error) {
  21. userID := auth.GetUserID(ctx.Request.Header)
  22. res, err := c.Repo.GetKey(ctx, userID)
  23. if err != nil {
  24. return nil, stacktrace.Propagate(err, "")
  25. }
  26. return &res, nil
  27. }
  28. // CreateEntity...
  29. func (c *Controller) CreateEntity(ctx *gin.Context, req model.CreateEntityRequest) (*model.Entity, error) {
  30. userID := auth.GetUserID(ctx.Request.Header)
  31. id, err := c.Repo.Create(ctx, userID, req)
  32. if err != nil {
  33. return nil, stacktrace.Propagate(err, "failed to createEntity")
  34. }
  35. entity, err := c.Repo.Get(ctx, userID, id)
  36. if err != nil {
  37. return nil, stacktrace.Propagate(err, "failed to createEntity")
  38. }
  39. return &entity, nil
  40. }
  41. // UpdateEntity...
  42. func (c *Controller) UpdateEntity(ctx *gin.Context, req model.UpdateEntityRequest) error {
  43. userID := auth.GetUserID(ctx.Request.Header)
  44. return c.Repo.Update(ctx, userID, req)
  45. }
  46. // Delete...
  47. func (c *Controller) Delete(ctx *gin.Context, entityID uuid.UUID) (bool, error) {
  48. userID := auth.GetUserID(ctx.Request.Header)
  49. return c.Repo.Delete(ctx, userID, entityID)
  50. }
  51. // GetDiff...
  52. func (c *Controller) GetDiff(ctx *gin.Context, req model.GetEntityDiffRequest) ([]model.Entity, error) {
  53. userID := auth.GetUserID(ctx.Request.Header)
  54. return c.Repo.GetDiff(ctx, userID, *req.SinceTime, req.Limit)
  55. }