2023-09-08 22:25:38 +07:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-09-12 17:07:57 +07:00
|
|
|
"database/sql"
|
2023-09-08 22:25:38 +07:00
|
|
|
"net/http"
|
2023-09-13 11:30:15 +07:00
|
|
|
"time"
|
2023-09-08 22:25:38 +07:00
|
|
|
|
2023-09-12 17:07:57 +07:00
|
|
|
db "git.nochill.in/nochill/hiling_go/db/sqlc"
|
|
|
|
"git.nochill.in/nochill/hiling_go/util"
|
2023-09-08 22:25:38 +07:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-09-12 17:07:57 +07:00
|
|
|
"github.com/lib/pq"
|
2023-09-08 22:25:38 +07:00
|
|
|
)
|
|
|
|
|
2023-09-12 17:07:57 +07:00
|
|
|
type createUserRequest struct {
|
|
|
|
Username string `json:"username" binding:"required,alphanum"`
|
|
|
|
Password string `json:"password" binding:"required,min=7"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type createUserResponse struct {
|
|
|
|
ID int32 `json:"id"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
AvatarPicture string `json:"avatar_picture"` // avatar_url
|
|
|
|
BannedAt sql.NullTime `json:"banned_at"`
|
|
|
|
BannedUntil sql.NullTime `json:"banned_until"`
|
|
|
|
BanReason string `json:"ban_reason"`
|
2023-09-13 11:30:15 +07:00
|
|
|
IsPermaban bool `json:"is_permaban"`
|
|
|
|
IsAdmin bool `json:"is_admin"`
|
|
|
|
IsCritics bool `json:"is_critics"`
|
|
|
|
IsVerified bool `json:"is_verified"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
UpdatedAt time.Time `json:"updated_at"`
|
2023-09-08 22:25:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) createUser(ctx *gin.Context) {
|
2023-09-12 17:07:57 +07:00
|
|
|
var req createUserRequest
|
2023-09-08 22:25:38 +07:00
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
2023-09-12 17:07:57 +07:00
|
|
|
if err != nil {
|
2023-09-13 11:30:15 +07:00
|
|
|
ctx.JSON(http.StatusBadRequest, ValidationErrorResponse(err))
|
2023-09-12 17:07:57 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hashedPassword, err := util.HashPassword(req.Password)
|
|
|
|
if err != nil {
|
|
|
|
ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong"))
|
2023-09-08 22:25:38 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-12 17:07:57 +07:00
|
|
|
arg := db.CreateUserParams{
|
|
|
|
Username: req.Username,
|
|
|
|
Password: hashedPassword,
|
|
|
|
}
|
|
|
|
|
2023-09-13 21:42:31 +07:00
|
|
|
user, err := server.Store.CreateUser(ctx, arg)
|
2023-09-08 22:25:38 +07:00
|
|
|
if err != nil {
|
2023-09-12 17:07:57 +07:00
|
|
|
if pqErr, ok := err.(*pq.Error); ok {
|
|
|
|
switch pqErr.Code.Name() {
|
|
|
|
case "foreign_key_violation", "unique_violation":
|
2023-09-13 11:30:15 +07:00
|
|
|
ctx.JSON(http.StatusConflict, ErrorResponse(err, "Username already used"))
|
|
|
|
return
|
2023-09-12 17:07:57 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong"))
|
2023-09-08 22:25:38 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-12 17:07:57 +07:00
|
|
|
res := createUserResponse{
|
|
|
|
ID: user.ID,
|
|
|
|
Username: user.Username,
|
|
|
|
AvatarPicture: user.AvatarPicture.String,
|
|
|
|
BannedAt: sql.NullTime{Valid: user.BannedAt.Valid, Time: user.BannedAt.Time},
|
|
|
|
BannedUntil: sql.NullTime{Valid: user.BannedUntil.Valid, Time: user.BannedUntil.Time},
|
|
|
|
BanReason: user.BanReason.String,
|
2023-09-13 11:30:15 +07:00
|
|
|
IsPermaban: user.IsPermaban.Bool,
|
|
|
|
IsAdmin: user.IsAdmin.Bool,
|
|
|
|
IsCritics: user.IsCritics.Bool,
|
|
|
|
IsVerified: user.IsVerified.Bool,
|
|
|
|
CreatedAt: user.CreatedAt.Time,
|
|
|
|
UpdatedAt: user.UpdatedAt.Time,
|
2023-09-12 17:07:57 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, res)
|
2023-09-08 22:25:38 +07:00
|
|
|
}
|