Skip to content
Snippets Groups Projects
Commit 063e184e authored by Alifia Rahmah's avatar Alifia Rahmah Committed by Bayu Samudra
Browse files

Feat/s1 sb12 implementing admin page

parent ca10cc42
Branches
Tags
2 merge requests!13Staging,!10Feat/s1 sb12 implementing admin page
Showing
with 461 additions and 4 deletions
......@@ -4,7 +4,7 @@ PORT=8080
LOGTAIL_TOKEN=
HTTP_TIMEOUT_SEC=2
LOG_FLUSH_INTERVAL_MS=1000
DB_STRING="host=localhost user=ocw password=ocw dbname=ocw-db port=5433 sslmode=disable TimeZone=Asia/Shanghai"
DB_STRING="host=localhost user=postgres password=postgres dbname=ocwdb port=5432 sslmode=disable TimeZone=Asia/Shanghai"
SMTP_USERNAME="noreply@ocw.id"
SMTP_SERVER=localhost
SMTP_PORT=1025
\ No newline at end of file
......@@ -35,6 +35,100 @@ const docTemplate = `{
}
}
},
"/admin/user": {
"get": {
"description": "Get all users from database",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "Get All User",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.BaseResponse"
}
}
}
},
"post": {
"description": "Add a user to database",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "Add User",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.BaseResponse"
}
}
}
},
"delete": {
"description": "Delete a user from database",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "Delete User By Id",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.BaseResponse"
}
}
}
},
"patch": {
"description": "Update a user from database",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "Update User By Id",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.BaseResponse"
}
}
}
}
},
"/admin/user/{id}": {
"get": {
"description": "Get a user from database",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "Get User By Email",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.BaseResponse"
}
}
}
}
},
"/auth/login": {
"post": {
"description": "Login and generate new pair of token",
......
......@@ -27,6 +27,100 @@
}
}
},
"/admin/user": {
"get": {
"description": "Get all users from database",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "Get All User",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.BaseResponse"
}
}
}
},
"post": {
"description": "Add a user to database",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "Add User",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.BaseResponse"
}
}
}
},
"delete": {
"description": "Delete a user from database",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "Delete User By Id",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.BaseResponse"
}
}
}
},
"patch": {
"description": "Update a user from database",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "Update User By Id",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.BaseResponse"
}
}
}
}
},
"/admin/user/{id}": {
"get": {
"description": "Get a user from database",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "Get User By Email",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/web.BaseResponse"
}
}
}
}
},
"/auth/login": {
"post": {
"description": "Login and generate new pair of token",
......
......@@ -86,6 +86,68 @@ paths:
summary: Index page
tags:
- common
/admin/user:
delete:
description: Delete a user from database
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/web.BaseResponse'
summary: Delete User By Id
tags:
- admin
get:
description: Get all users from database
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/web.BaseResponse'
summary: Get All User
tags:
- admin
patch:
description: Update a user from database
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/web.BaseResponse'
summary: Update User By Id
tags:
- admin
post:
description: Add a user to database
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/web.BaseResponse'
summary: Add User
tags:
- admin
/admin/user/{id}:
get:
description: Get a user from database
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/web.BaseResponse'
summary: Get User By Email
tags:
- admin
/auth/login:
post:
consumes:
......
package admin
import (
"net/http"
)
// Index godoc
//
// @Tags admin
// @Summary Add User
// @Description Add a user to database
// @Produce json
// @Success 200 {object} web.BaseResponse
// @Router /admin/user [post]
func (route AdminHandlerImpl) AddUser(w http.ResponseWriter, r *http.Request){
payload := route.WrapperUtil.SuccessResponseWrap(route.AdminService.AddUser())
route.HttpUtil.WriteSuccessJson(w, payload)
}
\ No newline at end of file
package admin
import (
"net/http"
)
// Index godoc
//
// @Tags admin
// @Summary Delete User By Id
// @Description Delete a user from database
// @Produce json
// @Success 200 {object} web.BaseResponse
// @Router /admin/user [delete]
func (route AdminHandlerImpl) DeleteUser(w http.ResponseWriter, r *http.Request){
payload := route.WrapperUtil.SuccessResponseWrap(route.AdminService.DeleteUser())
route.HttpUtil.WriteSuccessJson(w, payload)
}
\ No newline at end of file
package admin
import (
"net/http"
)
// Index godoc
//
// @Tags admin
// @Summary Get All User
// @Description Get all users from database
// @Produce json
// @Success 200 {object} web.BaseResponse
// @Router /admin/user [get]
func (route AdminHandlerImpl) GetAllUser(w http.ResponseWriter, r *http.Request){
// get all user from service
users, err := route.AdminService.GetAllUser()
if err != nil {
payload := route.WrapperUtil.ErrorResponseWrap(err.Error(), nil)
route.HttpUtil.WriteJson(w, http.StatusBadRequest, payload)
return
}
// wrap the response
payload := route.WrapperUtil.SuccessResponseWrap(users)
route.HttpUtil.WriteSuccessJson(w, payload)
}
\ No newline at end of file
package admin
import (
"net/http"
)
// Index godoc
//
// @Tags admin
// @Summary Get User By Email
// @Description Get a user from database
// @Produce json
// @Success 200 {object} web.BaseResponse
// @Router /admin/user/{id} [get]
func (route AdminHandlerImpl) GetUserByEmail(w http.ResponseWriter, r *http.Request) {
payload := route.WrapperUtil.SuccessResponseWrap(route.AdminService.GetUserByEmail())
route.HttpUtil.WriteSuccessJson(w, payload)
}
package admin
import (
"gitlab.informatika.org/ocw/ocw-backend/service/admin"
"gitlab.informatika.org/ocw/ocw-backend/utils/httputil"
"gitlab.informatika.org/ocw/ocw-backend/utils/wrapper"
)
type AdminHandlerImpl struct {
admin.AdminService
httputil.HttpUtil
wrapper.WrapperUtil
}
package admin
import "net/http"
type AdminHandler interface {
GetAllUser(w http.ResponseWriter, r *http.Request)
GetUserByEmail(w http.ResponseWriter, r *http.Request)
AddUser(w http.ResponseWriter, r *http.Request)
UpdateUser(w http.ResponseWriter, r *http.Request)
DeleteUser(w http.ResponseWriter, r *http.Request)
}
package admin
import (
"net/http"
)
// Index godoc
//
// @Tags admin
// @Summary Update User By Id
// @Description Update a user from database
// @Produce json
// @Success 200 {object} web.BaseResponse
// @Router /admin/user [patch]
func (route AdminHandlerImpl) UpdateUser(w http.ResponseWriter, r *http.Request){
payload := route.WrapperUtil.SuccessResponseWrap(route.AdminService.UpdateUser())
route.HttpUtil.WriteSuccessJson(w, payload)
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package handler
import (
"github.com/google/wire"
"gitlab.informatika.org/ocw/ocw-backend/handler/admin"
"gitlab.informatika.org/ocw/ocw-backend/handler/auth"
"gitlab.informatika.org/ocw/ocw-backend/handler/common"
"gitlab.informatika.org/ocw/ocw-backend/handler/swagger"
......@@ -15,7 +16,11 @@ var HandlerSet = wire.NewSet(
// Swagger
wire.Struct(new(swagger.SwaggerHandlerImpl), "*"),
wire.Bind(new(swagger.SwaggerHandler), new(*swagger.SwaggerHandlerImpl)),
// Admin
wire.Struct(new(admin.AdminHandlerImpl), "*"),
wire.Bind(new(admin.AdminHandler), new(*admin.AdminHandlerImpl)),
// Auth
wire.Struct(new(auth.AuthHandlerImpl), "*"),
wire.Bind(new(auth.AuthHandler), new(*auth.AuthHandlerImpl)),
......
......@@ -7,6 +7,7 @@ import (
type UserRepository interface {
Add(user user.User) error
Get(username string) (*user.User, error)
GetAll() ([]user.User, error)
Update(user user.User) error
Delete(username string) error
IsExist(user string) (bool, error)
......
......@@ -47,10 +47,21 @@ func (repo UserRepositoryImpl) Get(email string) (*user.User, error) {
return result, nil
}
func (repo UserRepositoryImpl) GetAll() ([]user.User, error) {
var result []user.User
err := repo.db.Find(&result).Error
if err != nil {
return nil, err
}
return result, nil
}
func (repo UserRepositoryImpl) Update(user user.User) error {
return repo.db.Save(user).Error
}
func (repo UserRepositoryImpl) Delete(email string) error {
return repo.db.Where("email = ?", email).Delete(&user.User{}).Error
func (repo UserRepositoryImpl) Delete(username string) error {
return repo.db.Where("username = ?", username).Delete(&user.User{}).Error
}
package admin
import (
"github.com/go-chi/chi/v5"
"gitlab.informatika.org/ocw/ocw-backend/handler/admin"
)
type AdminRoutes struct {
admin.AdminHandler
}
func (adr AdminRoutes) Register(r chi.Router) {
r.Route("/admin", func(r chi.Router) {
r.Get("/user", adr.AdminHandler.GetAllUser)
r.Get("/user/{id}", adr.AdminHandler.GetUserByEmail)
r.Post("/user", adr.AdminHandler.AddUser)
r.Patch("/user/{id}", adr.AdminHandler.UpdateUser)
r.Delete("/user/{id}", adr.AdminHandler.DeleteUser)
})
}
......@@ -2,6 +2,7 @@ package routes
import (
"github.com/google/wire"
"gitlab.informatika.org/ocw/ocw-backend/routes/admin"
"gitlab.informatika.org/ocw/ocw-backend/routes/auth"
"gitlab.informatika.org/ocw/ocw-backend/routes/common"
"gitlab.informatika.org/ocw/ocw-backend/routes/swagger"
......@@ -11,6 +12,7 @@ var routesCollectionSet = wire.NewSet(
wire.Struct(new(common.CommonRoutes), "*"),
wire.Struct(new(swagger.SwaggerRoutes), "*"),
wire.Struct(new(auth.AuthRoutes), "*"),
wire.Struct(new(admin.AdminRoutes), "*"),
)
var RoutesSet = wire.NewSet(
......
package routes
import (
"gitlab.informatika.org/ocw/ocw-backend/routes/admin"
"gitlab.informatika.org/ocw/ocw-backend/routes/auth"
"gitlab.informatika.org/ocw/ocw-backend/routes/common"
"gitlab.informatika.org/ocw/ocw-backend/routes/swagger"
......@@ -11,6 +12,7 @@ import (
type AppRouter struct {
// Routes
swagger.SwaggerRoutes
admin.AdminRoutes
common.CommonRoutes
auth.AuthRoutes
......
package admin
// import (
// "errors"
// "time"
// "github.com/golang-jwt/jwt/v4"
// "gitlab.informatika.org/ocw/ocw-backend/model/web"
// "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/login"
// tokenModel "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/token"
// "gorm.io/gorm"
// )
func (AdminServiceImpl) AddUser() string {
return "add user"
}
\ No newline at end of file
package admin
// import (
// "errors"
// "time"
// "github.com/golang-jwt/jwt/v4"
// "gitlab.informatika.org/ocw/ocw-backend/model/web"
// "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/login"
// tokenModel "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/token"
// "gorm.io/gorm"
// )
func (AdminServiceImpl) DeleteUser() string {
return "delete user"
}
package admin
import (
"gitlab.informatika.org/ocw/ocw-backend/model/domain/user"
)
func (as AdminServiceImpl) GetAllUser() ([]user.User, error) {
var users []user.User
users, nil := as.UserRepository.GetAll()
return users, nil
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment