diff --git a/.env b/.env index 1f5f0f141e9c1391bcd5b0325fbb516f0fa4bbfb..82b15d176cf462dcfa34cb48b3330b8ed01c8094 100644 --- a/.env +++ b/.env @@ -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 diff --git a/docs/docs.go b/docs/docs.go index 90d0363f2854b663778cdd7df051648418a59c2c..6fece1a6822d55bcc98a20a155a4039e5fb1a903 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -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", diff --git a/docs/swagger.json b/docs/swagger.json index dc9d78a343021dfa865bb7bc624edde2ff07b313..513bd4b6b3f4b57241fb86bbb6eb81d7ed0bd1b1 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 9c74543416b9ad2d2cb3b32c1ad3cfc340f575e9..ee76e9375fe026003fafa69a41451de0804766b5 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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: diff --git a/handler/admin/addUser.go b/handler/admin/addUser.go new file mode 100644 index 0000000000000000000000000000000000000000..7b53f3d3d4977c157d67048d3079c40e46e446fe --- /dev/null +++ b/handler/admin/addUser.go @@ -0,0 +1,18 @@ +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 diff --git a/handler/admin/deleteUser.go b/handler/admin/deleteUser.go new file mode 100644 index 0000000000000000000000000000000000000000..0be96ccc00451aaf0dd1490d6b29154a1b2d62dd --- /dev/null +++ b/handler/admin/deleteUser.go @@ -0,0 +1,18 @@ +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 diff --git a/handler/admin/getAllUser.go b/handler/admin/getAllUser.go new file mode 100644 index 0000000000000000000000000000000000000000..32771d8d99fcf9d82f53279bd70706a35d30f1b7 --- /dev/null +++ b/handler/admin/getAllUser.go @@ -0,0 +1,27 @@ +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 diff --git a/handler/admin/getUserByEmail.go b/handler/admin/getUserByEmail.go new file mode 100644 index 0000000000000000000000000000000000000000..0ad7a59fce2214548fd0aaae8b2443e4b5595ca7 --- /dev/null +++ b/handler/admin/getUserByEmail.go @@ -0,0 +1,18 @@ +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) +} diff --git a/handler/admin/handler.go b/handler/admin/handler.go new file mode 100644 index 0000000000000000000000000000000000000000..ac705969f7b899ea3b8749d288ddf047ea01d7dc --- /dev/null +++ b/handler/admin/handler.go @@ -0,0 +1,13 @@ +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 +} diff --git a/handler/admin/types.go b/handler/admin/types.go new file mode 100644 index 0000000000000000000000000000000000000000..2f30c48ef83a3af3db9f4b5f71e8cfc5653c9166 --- /dev/null +++ b/handler/admin/types.go @@ -0,0 +1,11 @@ +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) +} diff --git a/handler/admin/updateUser.go b/handler/admin/updateUser.go new file mode 100644 index 0000000000000000000000000000000000000000..c6531ec04835aabca40eedf74f3f008bc27d017c --- /dev/null +++ b/handler/admin/updateUser.go @@ -0,0 +1,18 @@ +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 diff --git a/handler/di.go b/handler/di.go index b07a5541580debe9eefda7f6979670ae3dd34696..ef48f67cd3b02eaa4df9d34dc17fb09fc317824b 100644 --- a/handler/di.go +++ b/handler/di.go @@ -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)), diff --git a/repository/user/type.go b/repository/user/type.go index 43ba1ea63681a21f2280674ff36bba5bc9db7146..77304dccc4c5ee44ac66d1e77af7d4144970b7ed 100644 --- a/repository/user/type.go +++ b/repository/user/type.go @@ -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) diff --git a/repository/user/user.go b/repository/user/user.go index 90858748ef449d00b5bfebf8fbe43f5c7564f343..f2919855b4b118e37a4afce174f83fceef2554dd 100644 --- a/repository/user/user.go +++ b/repository/user/user.go @@ -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 } diff --git a/routes/admin/route.go b/routes/admin/route.go new file mode 100644 index 0000000000000000000000000000000000000000..01f19943524626adb4aea1b73f5a912d4fdee3fa --- /dev/null +++ b/routes/admin/route.go @@ -0,0 +1,20 @@ +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) + }) +} diff --git a/routes/di.go b/routes/di.go index ad350ce824b581f436f168d7c2f2c38882e1c41f..51ee85b71e73dbda4683d1e4a8cc620d088c8e3a 100644 --- a/routes/di.go +++ b/routes/di.go @@ -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( diff --git a/routes/routes.go b/routes/routes.go index c824d2db4b82e25a80bff0bddf83f3bf30905c35..b29a111f5ffa31d00b6ad6615d0d72f399966a79 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,6 +1,7 @@ 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 diff --git a/service/admin/addUser.go b/service/admin/addUser.go new file mode 100644 index 0000000000000000000000000000000000000000..644bde9d0547485f4eb3ef45a4876d12327550c6 --- /dev/null +++ b/service/admin/addUser.go @@ -0,0 +1,16 @@ +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 diff --git a/service/admin/deleteUser.go b/service/admin/deleteUser.go new file mode 100644 index 0000000000000000000000000000000000000000..0e34cf7227863d1865c5de527b1bfa12806c8573 --- /dev/null +++ b/service/admin/deleteUser.go @@ -0,0 +1,16 @@ +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" +} diff --git a/service/admin/getAllUser.go b/service/admin/getAllUser.go new file mode 100644 index 0000000000000000000000000000000000000000..0977c267a8cbd463bc359a8b496105690cead99e --- /dev/null +++ b/service/admin/getAllUser.go @@ -0,0 +1,11 @@ +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 diff --git a/service/admin/getUserByEmail.go b/service/admin/getUserByEmail.go new file mode 100644 index 0000000000000000000000000000000000000000..e577536e5acccc895a4861a6845d9cd3b13018d8 --- /dev/null +++ b/service/admin/getUserByEmail.go @@ -0,0 +1,16 @@ +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) GetUserByEmail() string { + return "get user by email" +} \ No newline at end of file diff --git a/service/admin/impl.go b/service/admin/impl.go new file mode 100644 index 0000000000000000000000000000000000000000..7bc0dd3364dc3f46e47d282c647bb26380d4a31c --- /dev/null +++ b/service/admin/impl.go @@ -0,0 +1,9 @@ +package admin + +import ( + "gitlab.informatika.org/ocw/ocw-backend/repository/user" +) + +type AdminServiceImpl struct { + UserRepository user.UserRepository +} \ No newline at end of file diff --git a/service/admin/type.go b/service/admin/type.go new file mode 100644 index 0000000000000000000000000000000000000000..d624c56274802d9fc9523aa092d21f1fed14a314 --- /dev/null +++ b/service/admin/type.go @@ -0,0 +1,13 @@ +package admin + +import ( + "gitlab.informatika.org/ocw/ocw-backend/model/domain/user" +) + +type AdminService interface { + GetAllUser() ([]user.User, error) + GetUserByEmail() string + AddUser() string + UpdateUser() string + DeleteUser() string +} diff --git a/service/admin/updateUser.go b/service/admin/updateUser.go new file mode 100644 index 0000000000000000000000000000000000000000..e3bce672ae924615b9eb4df9ebda6d12c8afad00 --- /dev/null +++ b/service/admin/updateUser.go @@ -0,0 +1,16 @@ +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) UpdateUser() string { + return "update user" +} \ No newline at end of file diff --git a/service/di.go b/service/di.go index ee7d5e8ddb1c2c9074da8855c60d664e0882f760..c8ca39249a908a62ba8ed3742e3215cce1bfe4e4 100644 --- a/service/di.go +++ b/service/di.go @@ -3,6 +3,7 @@ package service import ( "github.com/google/wire" "gitlab.informatika.org/ocw/ocw-backend/service/auth" + "gitlab.informatika.org/ocw/ocw-backend/service/admin" "gitlab.informatika.org/ocw/ocw-backend/service/common" "gitlab.informatika.org/ocw/ocw-backend/service/logger" "gitlab.informatika.org/ocw/ocw-backend/service/logger/hooks" @@ -29,6 +30,12 @@ var ServiceTestSet = wire.NewSet( wire.Bind(new(auth.AuthService), new(*auth.AuthServiceImpl)), ), + // admin service + wire.NewSet( + wire.Struct(new(admin.AdminServiceImpl), "*"), + wire.Bind(new(admin.AdminService), new(*admin.AdminServiceImpl)), + ), + // verification service wire.NewSet( wire.Struct(new(verification.VerificationServiceImpl), "*"),