169 lines
4.5 KiB
Go
169 lines
4.5 KiB
Go
package user
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"template_blueprint/internal/database"
|
|
"template_blueprint/pkg/database/mongo"
|
|
"template_blueprint/pkg/models/user"
|
|
)
|
|
|
|
// InsertUser godoc
|
|
// @Summary Insert a new user
|
|
// @Description Adds a new user to the database
|
|
// @Tags users
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body user.ReqInsertUser true "User Data"
|
|
// @Success 200 {object} map[string]string "Successfully Inserted User"
|
|
// @Failure 400 {object} map[string]string "Bad Request"
|
|
// @Failure 500 {object} map[string]string "Database connection failed"
|
|
// @Router /api/localinsertuser [post]
|
|
func InsertUser(c *gin.Context) {
|
|
local := os.Getenv("BLUEPRINT_DB_LOCAL")
|
|
db := database.New(local).GetDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
mongoDB := mongo.NewDatabaseService(db)
|
|
var reqInsert user.ReqInsertUser
|
|
|
|
errBind := c.Bind(&reqInsert)
|
|
if errBind != nil {
|
|
c.JSON(400, gin.H{
|
|
"message": errBind.Error(),
|
|
})
|
|
return
|
|
}
|
|
id := uuid.New().String()
|
|
reqInsert.ID = id
|
|
errInsert := mongoDB.InsertUser(reqInsert)
|
|
if errInsert != nil {
|
|
c.JSON(400, gin.H{
|
|
"message": errInsert.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Successfully Inserted User"})
|
|
}
|
|
|
|
// GetAllUser godoc
|
|
// @Summary Get all users
|
|
// @Description Retrieves all users from the database
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Success 200 {array} user.User "List of users"
|
|
// @Failure 500 {object} map[string]string "Database connection failed"
|
|
// @Router /api/local/getalluser [get]
|
|
func GetAllUser(c *gin.Context) {
|
|
local := os.Getenv("BLUEPRINT_DB_LOCAL")
|
|
db := database.New(local).GetDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
mongoDB := mongo.NewDatabaseService(db)
|
|
dataUser, errSelect := mongoDB.GetAllDataUser()
|
|
if errSelect != nil {
|
|
c.JSON(400, gin.H{
|
|
"message": errSelect.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, dataUser)
|
|
}
|
|
|
|
// GetUserByID godoc
|
|
// @Summary Get user by ID
|
|
// @Description Retrieves a user by their ID
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param id path string true "User ID"
|
|
// @Success 200 {object} user.User "User data"
|
|
// @Failure 400 {object} map[string]string "Bad Request"
|
|
// @Router /api/local/getuser/{id} [get]
|
|
func GetUserByID(c *gin.Context) {
|
|
local := os.Getenv("BLUEPRINT_DB_LOCAL")
|
|
db := database.New(local).GetDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
id := c.Param("id")
|
|
log.Println("ID", id)
|
|
mongoDB := mongo.NewDatabaseService(db)
|
|
dataUser, errSelect := mongoDB.GetUserById(id)
|
|
if errSelect != nil {
|
|
c.JSON(400, gin.H{
|
|
"message": errSelect.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, dataUser)
|
|
}
|
|
|
|
// UpdateUser godoc
|
|
// @Summary Update a user
|
|
// @Description Updates user information
|
|
// @Tags users
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body user.User true "User Data"
|
|
// @Success 200 {object} map[string]string "Successfully Updated User"
|
|
// @Failure 400 {object} map[string]string "Bad Request"
|
|
// @Failure 500 {object} map[string]string "Database connection failed"
|
|
// @Router /users [put]
|
|
func UpdateUser(c *gin.Context) {
|
|
local := os.Getenv("BLUEPRINT_DB_LOCAL")
|
|
db := database.New(local).GetDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
mongoDB := mongo.NewDatabaseService(db)
|
|
var reqUpdate *user.ReqUpdateUser
|
|
errBind := c.Bind(&reqUpdate)
|
|
if errBind != nil {
|
|
c.JSON(400, gin.H{
|
|
"message": errBind.Error(),
|
|
})
|
|
return
|
|
}
|
|
log.Println("REQ UPDATE", reqUpdate)
|
|
errUpdate := mongoDB.UpdateUser(reqUpdate)
|
|
if errUpdate != nil {
|
|
c.JSON(400, gin.H{
|
|
"message": errUpdate.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Successfully Updated User"})
|
|
}
|
|
|
|
func DeleteUser(c *gin.Context) {
|
|
local := os.Getenv("BLUEPRINT_DB_LOCAL")
|
|
db := database.New(local).GetDB()
|
|
if db == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"message": "Database connection failed"})
|
|
return
|
|
}
|
|
mongoDB := mongo.NewDatabaseService(db)
|
|
var reqDelete *user.ReqDeleteUser
|
|
errBind := c.Bind(&reqDelete)
|
|
if errBind != nil {
|
|
c.JSON(400, gin.H{})
|
|
return
|
|
}
|
|
log.Println("REQ DELETE", reqDelete)
|
|
errDelete := mongoDB.DeleteUserById(reqDelete.ID)
|
|
if errDelete != nil {
|
|
c.JSON(400, gin.H{"message": "Failed to Delete User"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Successfully Deleted User"})
|
|
}
|