naise_pos/api/user.go

148 lines
3.8 KiB
Go
Raw Normal View History

2023-03-13 09:24:59 +07:00
package api
import (
"database/sql"
"net/http"
db "git.nochill.in/nochill/naice_pos/db/sqlc"
2023-03-14 12:04:03 +07:00
"git.nochill.in/nochill/naice_pos/util"
2023-03-13 09:24:59 +07:00
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/lib/pq"
)
2023-03-14 12:04:03 +07:00
type createUserMerchantRequest struct {
2023-03-13 09:24:59 +07:00
Email string `json:"email" binding:"required,email"`
2023-03-14 17:39:40 +07:00
Fullname string `json:"fullname" binding:"required,alphanum"`
2023-03-13 09:24:59 +07:00
Password string `json:"password" binding:"required"`
2023-03-14 17:39:40 +07:00
OutletName string `json:"outlet_name" binding:"required,alphanum"`
2023-03-13 09:24:59 +07:00
}
2023-03-14 17:39:40 +07:00
type userMerchantResponse struct {
2023-03-13 09:24:59 +07:00
ID uuid.UUID `json:"id"`
IndexID int64 `json:"index_id"`
Email string `json:"email"`
Fullname string `json:"fullname"`
CreatedAt sql.NullTime `json:"created_at"`
UpdatedAt sql.NullTime `json:"updated_at"`
Outlet db.Merchant `json:"outlet"`
}
2023-03-14 17:39:40 +07:00
func newUserMerchantResponse(user db.User, merchant db.Merchant) userMerchantResponse {
return userMerchantResponse{
ID: user.ID,
IndexID: user.IndexID,
Email: user.Email,
Fullname: user.Fullname,
CreatedAt: sql.NullTime{Valid: true, Time: user.CreatedAt.Time},
UpdatedAt: sql.NullTime{Valid: true, Time: user.UpdatedAt.Time},
Outlet: merchant,
}
}
2023-03-14 12:04:03 +07:00
func (server *Server) createUserMerchant(ctx *gin.Context) {
var req createUserMerchantRequest
2023-03-13 09:24:59 +07:00
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}
2023-03-14 12:04:03 +07:00
hashedPassword, err := util.HashPassword(req.Password)
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}
2023-03-13 09:24:59 +07:00
arg := db.UserMerchantTxParams{
Email: req.Email,
Fullname: req.Fullname,
2023-03-14 12:04:03 +07:00
Password: hashedPassword,
2023-03-13 09:24:59 +07:00
OutletName: req.OutletName,
}
user, err := server.store.CreateUserMerchantTx(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))
return
}
}
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}
2023-03-14 17:39:40 +07:00
res := userMerchantResponse{
2023-03-13 09:24:59 +07:00
ID: user.OwnerProfile.ID,
IndexID: user.OwnerProfile.IndexID,
Email: user.OwnerProfile.Email,
Fullname: user.OwnerProfile.Fullname,
2023-03-14 17:39:40 +07:00
CreatedAt: sql.NullTime{Valid: true, Time: user.OwnerProfile.CreatedAt.Time},
UpdatedAt: sql.NullTime{Valid: true, Time: user.OwnerProfile.UpdatedAt.Time},
2023-03-13 09:24:59 +07:00
Outlet: user.OutletProfile,
}
ctx.JSON(http.StatusOK, res)
}
2023-03-14 17:39:40 +07:00
type userLoginRequest struct {
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required"`
}
type userLoginResponse struct {
AccesToken string `json:"access_token"`
UserMerchantResponse userMerchantResponse
}
func (server *Server) loginUser(ctx *gin.Context) {
var req userLoginRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}
user, err := server.store.GetUserByEmail(ctx, req.Email)
if err != nil {
if err == sql.ErrNoRows {
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}
outlet, err := server.store.GetMerchantByUserId(ctx, user.ID)
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}
err = util.CheckPassword(req.Password, user.Password)
if err != nil {
ctx.JSON(http.StatusUnauthorized, errorResponse(err))
return
}
accessToken, err := server.tokenMaker.CreateToken(
user.Email,
2023-03-15 15:00:36 +07:00
outlet.ID.String(),
2023-03-14 17:39:40 +07:00
server.config.TokenDuration,
)
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}
userMerchant := newUserMerchantResponse(user, outlet)
res := userLoginResponse{
AccesToken: accessToken,
UserMerchantResponse: userMerchant,
}
ctx.JSON(http.StatusOK, res)
}