-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from gen-mind/develop
develop
- Loading branch information
Showing
48 changed files
with
2,915 additions
and
411 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package handler | ||
|
||
import ( | ||
"cognix.ch/api/v2/core/bll" | ||
"cognix.ch/api/v2/core/parameters" | ||
"cognix.ch/api/v2/core/security" | ||
"cognix.ch/api/v2/core/server" | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"mime/multipart" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
type DocumentHandler struct { | ||
documentBL bll.DocumentBL | ||
} | ||
|
||
func NewDocumentHandler(documentBL bll.DocumentBL) *DocumentHandler { | ||
return &DocumentHandler{documentBL: documentBL} | ||
} | ||
|
||
func (h *DocumentHandler) Mount(router *gin.Engine, authMiddleware gin.HandlerFunc) { | ||
handler := router.Group("/manage/documents").Use(authMiddleware) | ||
handler.GET("/", server.HandlerErrorFuncAuth(h.GetAll)) | ||
handler.POST("/upload", server.HandlerErrorFuncAuth(h.Upload)) | ||
} | ||
|
||
func (h *DocumentHandler) GetAll(c *gin.Context, identity *security.Identity) error { | ||
return nil | ||
} | ||
|
||
func (h *DocumentHandler) Upload(c *gin.Context, identity *security.Identity) error { | ||
form, _ := c.MultipartForm() | ||
files := form.File["upload[]"] | ||
|
||
wg := sync.WaitGroup{} | ||
result := make([]*parameters.DocumentUploadResponse, len(files)) | ||
for i, f := range files { | ||
wg.Add(1) | ||
go func(idx int, file multipart.FileHeader) { | ||
defer wg.Done() | ||
fileReader, err := file.Open() | ||
contentType := file.Header.Get("Content-Type") | ||
if err != nil { | ||
result[idx] = ¶meters.DocumentUploadResponse{ | ||
FileName: file.Filename, | ||
Error: fmt.Sprintf("open file : %s", err.Error()), | ||
Document: nil, | ||
} | ||
return | ||
} | ||
defer fileReader.Close() | ||
document, err := h.documentBL.UploadDocument(c.Request.Context(), identity.User, file.Filename, contentType, fileReader) | ||
if err != nil { | ||
result[idx] = ¶meters.DocumentUploadResponse{ | ||
FileName: file.Filename, | ||
Error: fmt.Sprintf("upload document : %s", err.Error()), | ||
Document: nil, | ||
} | ||
return | ||
} | ||
result[idx] = ¶meters.DocumentUploadResponse{ | ||
FileName: file.Filename, | ||
Error: "", | ||
Document: document, | ||
} | ||
}(i, *f) | ||
} | ||
wg.Wait() | ||
return server.JsonResult(c, http.StatusOK, result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package handler | ||
|
||
import ( | ||
"cognix.ch/api/v2/core/bll" | ||
"cognix.ch/api/v2/core/model" | ||
"cognix.ch/api/v2/core/parameters" | ||
"cognix.ch/api/v2/core/security" | ||
"cognix.ch/api/v2/core/server" | ||
"cognix.ch/api/v2/core/utils" | ||
"github.com/gin-gonic/gin" | ||
"github.com/google/uuid" | ||
"net/http" | ||
) | ||
|
||
// TenantHandler handles authentication endpoints | ||
type TenantHandler struct { | ||
tenantBL bll.TenantBL | ||
} | ||
|
||
func NewTenantHandler(TenantBL bll.TenantBL) *TenantHandler { | ||
return &TenantHandler{ | ||
tenantBL: TenantBL, | ||
} | ||
} | ||
|
||
func (h *TenantHandler) Mount(route *gin.Engine, TenantMiddleware gin.HandlerFunc) { | ||
handler := route.Group("/tenant") | ||
handler.Use(TenantMiddleware) | ||
handler.GET("/users", server.HandlerErrorFuncAuth(h.GetUserList)) | ||
handler.POST("/users", server.HandlerErrorFuncAuth(h.AddUser)) | ||
handler.PUT("/users/:id", server.HandlerErrorFuncAuth(h.EditUser)) | ||
} | ||
|
||
// GetUserList return list of users | ||
// @Summary return list of users | ||
// @Description return list of users | ||
// @Tags Tenant | ||
// @ID tenant_get_users | ||
// @Produce json | ||
// @Security ApiKeyAuth | ||
// @Success 200 {array} model.User | ||
// @Router /tenant/users [get] | ||
func (h *TenantHandler) GetUserList(c *gin.Context, identity *security.Identity) error { | ||
users, err := h.tenantBL.GetUsers(c.Request.Context(), identity.User) | ||
if err != nil { | ||
return err | ||
} | ||
return server.JsonResult(c, http.StatusOK, users) | ||
} | ||
|
||
// AddUser add new user | ||
// @Summary add new user | ||
// @Description add new user | ||
// @Tags Tenant | ||
// @ID tenant_add_user | ||
// @Param params body parameters.AddUserParam true "create user parameter" | ||
// @Produce json | ||
// @Security ApiKeyAuth | ||
// @Success 200 {object} model.User | ||
// @Router /tenant/users [post] | ||
func (h *TenantHandler) AddUser(c *gin.Context, identity *security.Identity) error { | ||
if !identity.User.HasRoles(model.RoleAdmin, model.RoleSuperAdmin) { | ||
return utils.ErrorPermission.New("do not have permission") | ||
} | ||
|
||
var param parameters.AddUserParam | ||
if err := c.ShouldBind(¶m); err != nil { | ||
return utils.InvalidInput.Wrap(err, "wrong parameter") | ||
} | ||
if err := param.Validate(); err != nil { | ||
return utils.InvalidInput.New(err.Error()) | ||
} | ||
user, err := h.tenantBL.AddUser(c.Request.Context(), identity.User, param.Email, param.Role) | ||
if err != nil { | ||
return err | ||
} | ||
return server.JsonResult(c, http.StatusOK, user) | ||
} | ||
|
||
// EditUser edit user | ||
// @Summary edit user | ||
// @Description edit user | ||
// @Tags Tenant | ||
// @ID tenant_edit_user | ||
// @Param id path string true "user id" | ||
// @Param params body parameters.EditUserParam true "edit user parameter" | ||
// @Produce json | ||
// @Security ApiKeyAuth | ||
// @Success 200 {object} model.User | ||
// @Router /tenant/users/{id} [put] | ||
func (h *TenantHandler) EditUser(c *gin.Context, identity *security.Identity) error { | ||
if !identity.User.HasRoles(model.RoleAdmin, model.RoleSuperAdmin) { | ||
return utils.ErrorPermission.New("do not have permission") | ||
} | ||
id := c.Param("id") | ||
userID, err := uuid.Parse(id) | ||
if err != nil { | ||
return utils.ErrorPermission.Wrap(err, "wrong id parameter") | ||
} | ||
var param parameters.EditUserParam | ||
if err := c.ShouldBind(¶m); err != nil { | ||
return utils.InvalidInput.Wrap(err, "wrong parameter") | ||
} | ||
if err := param.Validate(); err != nil { | ||
return utils.InvalidInput.New(err.Error()) | ||
} | ||
user, err := h.tenantBL.UpdateUser(c.Request.Context(), identity.User, userID, param.Role) | ||
if err != nil { | ||
return err | ||
} | ||
return server.JsonResult(c, http.StatusOK, user) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.