|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "sandbox-go-api-sqlboiler-rest-auth/models" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/volatiletech/sqlboiler/v4/boil" |
| 11 | + |
| 12 | + "github.com/volatiletech/sqlboiler/v4/queries/qm" |
| 13 | + |
| 14 | + "github.com/labstack/echo/v4" |
| 15 | +) |
| 16 | + |
| 17 | +type PublicUser struct { |
| 18 | + ID int `boil:"id" json:"id"` |
| 19 | + Email string `boil:"email" json:"email"` |
| 20 | + CreatedAt time.Time `boil:"created_at" json:"created_at"` |
| 21 | + UpdatedAt time.Time `boil:"updated_at" json:"updated_at"` |
| 22 | +} |
| 23 | + |
| 24 | +type UsersData struct { |
| 25 | + Users *[]PublicUser `json:"users"` |
| 26 | +} |
| 27 | + |
| 28 | +func (h *Handlers) GetUsers(c echo.Context) error { |
| 29 | + ctx := context.Background() |
| 30 | + var users []PublicUser |
| 31 | + res := SuccessResponse{ |
| 32 | + Data: UsersData{Users: &users}, |
| 33 | + } |
| 34 | + err := models.Users().Bind(ctx, h.db, &users) |
| 35 | + if err != nil { |
| 36 | + c.Error(err) |
| 37 | + return err |
| 38 | + } |
| 39 | + return c.JSON(http.StatusOK, res) |
| 40 | +} |
| 41 | + |
| 42 | +type UserData struct { |
| 43 | + User *PublicUser `json:"user"` |
| 44 | +} |
| 45 | + |
| 46 | +func (h *Handlers) GetUser(c echo.Context) error { |
| 47 | + ctx := context.Background() |
| 48 | + res := SuccessResponse{ |
| 49 | + Data: UserData{}, |
| 50 | + } |
| 51 | + var id int |
| 52 | + err := echo.PathParamsBinder(c). |
| 53 | + Int("id", &id). |
| 54 | + BindError() |
| 55 | + if err != nil { |
| 56 | + return echo.NewHTTPError(http.StatusBadRequest, err.Error()) |
| 57 | + } |
| 58 | + |
| 59 | + exists, err := models.UserExists(ctx, h.db, id) |
| 60 | + if !exists { |
| 61 | + return echo.NewHTTPError(http.StatusNotFound, http.StatusText(http.StatusNotFound)) |
| 62 | + } |
| 63 | + if err != nil { |
| 64 | + return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) |
| 65 | + } |
| 66 | + |
| 67 | + var users []*PublicUser |
| 68 | + err = models.Users(qm.Where("id = ?", id)).Bind(ctx, h.db, &users) |
| 69 | + if err != nil { |
| 70 | + c.Error(err) |
| 71 | + return err |
| 72 | + } |
| 73 | + res.Data = &users[0] |
| 74 | + |
| 75 | + return c.JSON(http.StatusOK, res) |
| 76 | +} |
| 77 | + |
| 78 | +type CreateUserRequest struct { |
| 79 | + Email string `json:"email"` |
| 80 | + Password string `json:"password"` |
| 81 | +} |
| 82 | + |
| 83 | +func (h *Handlers) CreateUser(c echo.Context) error { |
| 84 | + ctx := context.Background() |
| 85 | + res := SuccessResponse{ |
| 86 | + Data: UserData{}, |
| 87 | + } |
| 88 | + req := new(CreateUserRequest) |
| 89 | + if err := c.Bind(req); err != nil { |
| 90 | + return echo.NewHTTPError(http.StatusBadRequest, err.Error()) |
| 91 | + } |
| 92 | + u := models.User{ |
| 93 | + Email: req.Email, |
| 94 | + HashedPassword: req.Password, |
| 95 | + } |
| 96 | + err := u.Insert(ctx, h.db, boil.Infer()) |
| 97 | + if err != nil { |
| 98 | + return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) |
| 99 | + } |
| 100 | + pu := PublicUser{ |
| 101 | + ID: u.ID, |
| 102 | + Email: u.Email, |
| 103 | + CreatedAt: u.CreatedAt, |
| 104 | + UpdatedAt: u.UpdatedAt, |
| 105 | + } |
| 106 | + res.Data = UserData{ |
| 107 | + User: &pu, |
| 108 | + } |
| 109 | + |
| 110 | + return c.JSON(http.StatusOK, res) |
| 111 | +} |
| 112 | + |
| 113 | +func (h *Handlers) PatchUser(c echo.Context) error { |
| 114 | + return errors.New("not implemented") |
| 115 | +} |
| 116 | + |
| 117 | +func (h *Handlers) DeleteUser(c echo.Context) error { |
| 118 | + ctx := context.Background() |
| 119 | + var id int |
| 120 | + err := echo.PathParamsBinder(c). |
| 121 | + Int("id", &id). |
| 122 | + BindError() |
| 123 | + if err != nil { |
| 124 | + return echo.NewHTTPError(http.StatusBadRequest, err.Error()) |
| 125 | + } |
| 126 | + exists, err := models.UserExists(ctx, h.db, id) |
| 127 | + if !exists { |
| 128 | + return echo.NewHTTPError(http.StatusNotFound, http.StatusText(http.StatusNotFound)) |
| 129 | + } |
| 130 | + if err != nil { |
| 131 | + return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) |
| 132 | + } |
| 133 | + u, err := models.FindUser(ctx, h.db, id) |
| 134 | + if err != nil { |
| 135 | + return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) |
| 136 | + } |
| 137 | + |
| 138 | + _, err = u.Delete(ctx, h.db) |
| 139 | + if err != nil { |
| 140 | + return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) |
| 141 | + } |
| 142 | + return c.NoContent(http.StatusOK) |
| 143 | +} |
0 commit comments