controller.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package authenticaor
  2. import (
  3. model "github.com/ente-io/museum/ente/userentity"
  4. "github.com/ente-io/museum/pkg/repo/userentity"
  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 *userentity.Repository
  13. }
  14. // CreateKey stores an entity key for the given type
  15. func (c *Controller) CreateKey(ctx *gin.Context, req model.EntityKeyRequest) 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, req model.GetEntityKeyRequest) (*model.EntityKey, error) {
  21. userID := auth.GetUserID(ctx.Request.Header)
  22. res, err := c.Repo.GetKey(ctx, userID, req.Type)
  23. if err != nil {
  24. return nil, stacktrace.Propagate(err, "")
  25. }
  26. return &res, nil
  27. }
  28. // CreateEntity stores entity data for the given type
  29. func (c *Controller) CreateEntity(ctx *gin.Context, req model.EntityDataRequest) (*model.EntityData, 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. return c.Repo.Get(ctx, userID, id)
  36. }
  37. // UpdateEntity...
  38. func (c *Controller) UpdateEntity(ctx *gin.Context, req model.UpdateEntityDataRequest) (*model.EntityData, error) {
  39. userID := auth.GetUserID(ctx.Request.Header)
  40. err := c.Repo.Update(ctx, userID, req)
  41. if err != nil {
  42. return nil, stacktrace.Propagate(err, "failed to updateEntity")
  43. }
  44. return c.Repo.Get(ctx, userID, req.ID)
  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 returns diff of EntityData for the given type
  52. func (c *Controller) GetDiff(ctx *gin.Context, req model.GetEntityDiffRequest) ([]model.EntityData, error) {
  53. userID := auth.GetUserID(ctx.Request.Header)
  54. return c.Repo.GetDiff(ctx, userID, req.Type, *req.SinceTime, req.Limit)
  55. }