feat: add feature list all role
This commit is contained in:
parent
5636d99dc9
commit
c326fcc6fc
|
@ -3,5 +3,5 @@ package domain
|
||||||
type UserRole struct {
|
type UserRole struct {
|
||||||
ID string `gorm:"primaryKey;type:uuid;default:uuid_generate_v4();unique;not null" json:"id"`
|
ID string `gorm:"primaryKey;type:uuid;default:uuid_generate_v4();unique;not null" json:"id"`
|
||||||
RoleName string `gorm:"unique;not null" json:"roleName"`
|
RoleName string `gorm:"unique;not null" json:"roleName"`
|
||||||
Users []User `gorm:"foreignKey:RoleID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"users"`
|
// Users []User `gorm:"foreignKey:RoleID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"users"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func AppRouter(app *fiber.App) {
|
func AppRouter(app *fiber.App) {
|
||||||
|
// # role
|
||||||
|
app.Get("/listrole", controllers.GetAllUserRoles)
|
||||||
|
app.Get("/listrole/:id", controllers.GetUserRoleByID)
|
||||||
|
|
||||||
// # authentication
|
// # authentication
|
||||||
app.Post("/register", controllers.Register)
|
app.Post("/register", controllers.Register)
|
||||||
app.Post("/login", controllers.Login)
|
app.Post("/login", controllers.Login)
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/internal/services"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetUserRoleByID(c *fiber.Ctx) error {
|
||||||
|
id := c.Params("id")
|
||||||
|
|
||||||
|
role, err := services.GetUserRoleByID(id)
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(fiber.StatusNotFound).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusNotFound,
|
||||||
|
"UserRole tidak ditemukan",
|
||||||
|
nil,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusOK,
|
||||||
|
"UserRole ditemukan",
|
||||||
|
role,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAllUserRoles(c *fiber.Ctx) error {
|
||||||
|
roles, err := services.GetAllUserRoles()
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusInternalServerError,
|
||||||
|
"Gagal mengambil data UserRole",
|
||||||
|
nil,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusOK,
|
||||||
|
"Daftar UserRole",
|
||||||
|
roles,
|
||||||
|
))
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/pahmiudahgede/senggoldong/config"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/domain"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetUserRoleByID(id string) (domain.UserRole, error) {
|
||||||
|
var role domain.UserRole
|
||||||
|
err := config.DB.Where("id = ?", id).First(&role).Error
|
||||||
|
if err != nil {
|
||||||
|
return role, errors.New("UserRole tidak ditemukan")
|
||||||
|
}
|
||||||
|
return role, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAllUserRoles() ([]domain.UserRole, error) {
|
||||||
|
var roles []domain.UserRole
|
||||||
|
err := config.DB.Find(&roles).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("Gagal mengambil data UserRole")
|
||||||
|
}
|
||||||
|
return roles, nil
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/pahmiudahgede/senggoldong/domain"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/internal/repositories"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetUserRoleByID(id string) (domain.UserRole, error) {
|
||||||
|
role, err := repositories.GetUserRoleByID(id)
|
||||||
|
if err != nil {
|
||||||
|
return role, errors.New("UserRole tidak ditemukan")
|
||||||
|
}
|
||||||
|
return role, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAllUserRoles() ([]domain.UserRole, error) {
|
||||||
|
roles, err := repositories.GetAllUserRoles()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("Gagal mengambil data UserRole")
|
||||||
|
}
|
||||||
|
return roles, nil
|
||||||
|
}
|
Loading…
Reference in New Issue