package api import ( "database/sql" "net/http" "time" db "git.nochill.in/nochill/hiling_go/db/sqlc" "git.nochill.in/nochill/hiling_go/util" "github.com/gin-gonic/gin" "github.com/lib/pq" ) 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"` 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"` } func (server *Server) createUser(ctx *gin.Context) { var req createUserRequest if err := ctx.ShouldBindJSON(&req); err != nil { if err != nil { ctx.JSON(http.StatusBadRequest, ValidationErrorResponse(err)) return } } hashedPassword, err := util.HashPassword(req.Password) if err != nil { ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong")) return } arg := db.CreateUserParams{ Username: req.Username, Password: hashedPassword, } user, err := server.store.CreateUser(ctx, arg) if err != nil { if pqErr, ok := err.(*pq.Error); ok { switch pqErr.Code.Name() { case "foreign_key_violation", "unique_violation": ctx.JSON(http.StatusConflict, ErrorResponse(err, "Username already used")) return } } ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong")) return } 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, IsPermaban: user.IsPermaban.Bool, IsAdmin: user.IsAdmin.Bool, IsCritics: user.IsCritics.Bool, IsVerified: user.IsVerified.Bool, CreatedAt: user.CreatedAt.Time, UpdatedAt: user.UpdatedAt.Time, } ctx.JSON(http.StatusOK, res) }