Skip to content

Commit

Permalink
Merge pull request #86 from gen-mind/develop
Browse files Browse the repository at this point in the history
develop
  • Loading branch information
apaladiychuk authored May 3, 2024
2 parents a0789dd + 399f764 commit 1ae52c7
Show file tree
Hide file tree
Showing 23 changed files with 134 additions and 93 deletions.
10 changes: 5 additions & 5 deletions backend/api/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (h *AuthHandler) Mount(route *gin.Engine, authMiddleware gin.HandlerFunc) {
func (h *AuthHandler) SignIn(c *gin.Context) error {
var param parameters.LoginParam
if err := c.ShouldBindQuery(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong redirect url")
return utils.ErrorBadRequest.Wrap(err, "wrong redirect url")
}
zap.S().Infof("redirec to %s", param.RedirectURL)
buf, err := json.Marshal(parameters.OAuthParam{Action: oauth.LoginState})
Expand Down Expand Up @@ -94,7 +94,7 @@ func (h *AuthHandler) Callback(c *gin.Context) error {

buf, err := base64.URLEncoding.DecodeString(c.Query(oauth.StateNameGoogle))
if err != nil {
return utils.InvalidInput.Wrap(err, "wrong state")
return utils.ErrorBadRequest.Wrap(err, "wrong state")
}
var state parameters.OAuthParam
if err = json.Unmarshal(buf, &state); err != nil {
Expand Down Expand Up @@ -169,10 +169,10 @@ func (h *AuthHandler) Callback(c *gin.Context) error {
//func (h *AuthHandler) Invite(c *gin.Context, identity *security.Identity) error {
// var param parameters.InviteParam
// if err := c.BindJSON(&param); err != nil {
// return utils.InvalidInput.Wrap(err, "can not parse payload")
// return utils.ErrorBadRequest.Wrap(err, "can not parse payload")
// }
// if err := param.Validate(); err != nil {
// return utils.InvalidInput.Wrap(err, err.Error())
// return utils.ErrorBadRequest.Wrap(err, err.Error())
// }
//
// url, err := h.authBL.Invite(c.Request.Context(), identity, &param)
Expand All @@ -195,7 +195,7 @@ func (h *AuthHandler) Callback(c *gin.Context) error {
// //
// //key, err := base64.URLEncoding.DecodeString(param)
// //if err != nil {
// // return utils.InvalidInput.Wrap(err, "wrong state")
// // return utils.ErrorBadRequest.Wrap(err, "wrong state")
// //}
// ////value, err := h.storage.Pull(string(key))
// //
Expand Down
17 changes: 7 additions & 10 deletions backend/api/handler/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (h *ChatHandler) GetSessions(c *gin.Context, identity *security.Identity) e
func (h *ChatHandler) GetByID(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
session, err := h.chatBL.GetSessionByID(c.Request.Context(), identity.User, id)
if err != nil {
Expand All @@ -82,11 +82,8 @@ func (h *ChatHandler) GetByID(c *gin.Context, identity *security.Identity) error
// @Router /chats/create-chat-session [post]
func (h *ChatHandler) CreateSession(c *gin.Context, identity *security.Identity) error {
var param parameters.CreateChatSession
if err := c.BindJSON(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong payload")
}
if err := param.Validate(); err != nil {
return utils.InvalidInput.Wrap(err, err.Error())
if err := server.BindJsonAndValidate(c, &param); err != nil {
return err
}
session, err := h.chatBL.CreateSession(c.Request.Context(), identity.User, &param)
if err != nil {
Expand All @@ -107,8 +104,8 @@ func (h *ChatHandler) CreateSession(c *gin.Context, identity *security.Identity)
// @Router /chats/send-message [post]
func (h *ChatHandler) SendMessage(c *gin.Context, identity *security.Identity) error {
var param parameters.CreateChatMessageRequest
if err := c.BindJSON(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong payload")
if err := server.BindJsonAndValidate(c, &param); err != nil {
return err
}
assistant, err := h.chatBL.SendMessage(c, identity.User, &param)
if err != nil {
Expand Down Expand Up @@ -138,10 +135,10 @@ func (h *ChatHandler) SendMessage(c *gin.Context, identity *security.Identity) e
func (h *ChatHandler) MessageFeedback(c *gin.Context, identity *security.Identity) error {
var param parameters.MessageFeedbackParam
if err := c.BindJSON(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong payload")
return utils.ErrorBadRequest.Wrap(err, "wrong payload")
}
if err := param.Validate(); err != nil {
return utils.InvalidInput.Wrap(err, err.Error())
return utils.ErrorBadRequest.Wrap(err, err.Error())
}
feedback, err := h.chatBL.FeedbackMessage(c, identity.User, param.ID.IntPart(), param.Vote == parameters.MessageFeedbackUpvote)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions backend/api/handler/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (h *ConnectorHandler) GetById(c *gin.Context) error {
}
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}

connectors, err := h.connectorBL.GetByID(c.Request.Context(), identity.User, id)
Expand All @@ -99,10 +99,10 @@ func (h *ConnectorHandler) Create(c *gin.Context) error {
}
var param parameters.CreateConnectorParam
if err = c.BindJSON(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong payload")
return utils.ErrorBadRequest.Wrap(err, "wrong payload")
}
if err = param.Validate(); err != nil {
return utils.InvalidInput.Wrap(err, err.Error())
return utils.ErrorBadRequest.Wrap(err, err.Error())
}
connector, err := h.connectorBL.Create(c.Request.Context(), identity.User, &param)
if err != nil {
Expand All @@ -129,11 +129,11 @@ func (h *ConnectorHandler) Update(c *gin.Context) error {
}
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
var param parameters.UpdateConnectorParam
if err = c.BindJSON(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong payload")
return utils.ErrorBadRequest.Wrap(err, "wrong payload")
}
connector, err := h.connectorBL.Update(c.Request.Context(), id, identity.User, &param)
if err != nil {
Expand Down Expand Up @@ -173,11 +173,11 @@ func (h *ConnectorHandler) GetSourceTypes(c *gin.Context, identity *security.Ide
func (h *ConnectorHandler) Archive(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
action := c.Param("action")
if !(action == ActionRestore || action == ActionDelete) {
return utils.InvalidInput.Newf("invalid action: should be %s or %s", ActionRestore, ActionDelete)
return utils.ErrorBadRequest.Newf("invalid action: should be %s or %s", ActionRestore, ActionDelete)
}
credential, err := h.connectorBL.Archive(c.Request.Context(), identity.User, id, action == ActionRestore)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions backend/api/handler/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (h *CredentialHandler) GetAll(c *gin.Context) error {
}
var param parameters.GetAllCredentialsParam
if err = c.BindQuery(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong parameters")
return utils.ErrorBadRequest.Wrap(err, "wrong parameters")
}
credentials, err := h.credentialBl.GetAll(c.Request.Context(), ident.User, &param)
if err != nil {
Expand All @@ -72,7 +72,7 @@ func (h *CredentialHandler) GetByID(c *gin.Context) error {
}
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}

credential, err := h.credentialBl.GetByID(c.Request.Context(), ident.User, id)
Expand All @@ -99,10 +99,10 @@ func (h *CredentialHandler) Create(c *gin.Context) error {
}
var param parameters.CreateCredentialParam
if err = c.BindJSON(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong payload")
return utils.ErrorBadRequest.Wrap(err, "wrong payload")
}
if err = param.Validate(); err != nil {
return utils.InvalidInput.Wrap(err, err.Error())
return utils.ErrorBadRequest.Wrap(err, err.Error())
}
credential, err := h.credentialBl.Create(c.Request.Context(), ident.User, &param)
if err != nil {
Expand All @@ -129,11 +129,11 @@ func (h *CredentialHandler) Update(c *gin.Context) error {
}
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
var param parameters.UpdateCredentialParam
if err = c.BindJSON(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong payload")
return utils.ErrorBadRequest.Wrap(err, "wrong payload")
}
credential, err := h.credentialBl.Update(c.Request.Context(), id, ident.User, &param)
if err != nil {
Expand All @@ -156,11 +156,11 @@ func (h *CredentialHandler) Update(c *gin.Context) error {
func (h *CredentialHandler) Archive(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
action := c.Param("action")
if !(action == ActionRestore || action == ActionDelete) {
return utils.InvalidInput.Newf("invalid action: should be %s or %s", ActionRestore, ActionDelete)
return utils.ErrorBadRequest.Newf("invalid action: should be %s or %s", ActionRestore, ActionDelete)
}
credential, err := h.credentialBl.Archive(c.Request.Context(), identity.User, id, action == ActionRestore)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions backend/api/handler/document_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (h *DocumentSetHandler) Mount(router *gin.Engine, authMiddleware gin.Handle
func (h *DocumentSetHandler) GetAll(c *gin.Context, identity *security.Identity) error {
var param parameters.ArchivedParam
if err := c.ShouldBindQuery(&param); err != nil {
return utils.InvalidInput.Wrap(err, "failed to bind query params")
return utils.ErrorBadRequest.Wrap(err, "failed to bind query params")
}
documentSets, err := h.documentSetBL.GetByUser(c.Request.Context(), identity.User, &param)
if err != nil {
Expand All @@ -66,7 +66,7 @@ func (h *DocumentSetHandler) GetAll(c *gin.Context, identity *security.Identity)
func (h *DocumentSetHandler) GetByID(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
documentSet, err := h.documentSetBL.GetByID(c, identity.User, id)
if err != nil {
Expand All @@ -89,7 +89,7 @@ func (h *DocumentSetHandler) GetByID(c *gin.Context, identity *security.Identity
func (h *DocumentSetHandler) Create(c *gin.Context, identity *security.Identity) error {
var param parameters.DocumentSetParam
if err := c.ShouldBindJSON(&param); err != nil {
return utils.InvalidInput.New("invalid params")
return utils.ErrorBadRequest.New("invalid params")
}
documentSet, err := h.documentSetBL.Create(c.Request.Context(), identity.User, &param)
if err != nil {
Expand All @@ -112,12 +112,12 @@ func (h *DocumentSetHandler) Create(c *gin.Context, identity *security.Identity)
func (h *DocumentSetHandler) Update(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}

var param parameters.DocumentSetParam
if err = c.ShouldBindJSON(&param); err != nil {
return utils.InvalidInput.New("invalid params")
return utils.ErrorBadRequest.New("invalid params")
}
documentSet, err := h.documentSetBL.Update(c.Request.Context(), identity.User, id, &param)
if err != nil {
Expand All @@ -140,11 +140,11 @@ func (h *DocumentSetHandler) Update(c *gin.Context, identity *security.Identity)
func (h *DocumentSetHandler) Delete(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
action := c.Param("action")
if !(action == ActionRestore || action == ActionDelete) {
return utils.InvalidInput.Newf("invalid action: should be %s or %s", ActionRestore, ActionDelete)
return utils.ErrorBadRequest.Newf("invalid action: should be %s or %s", ActionRestore, ActionDelete)
}
var documentSet *model.DocumentSet
switch action {
Expand Down
14 changes: 7 additions & 7 deletions backend/api/handler/embedding_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (h *EmbeddingModelHandler) Mount(router *gin.Engine, authMiddleware gin.Han
func (h *EmbeddingModelHandler) GetAll(c *gin.Context, identity *security.Identity) error {
var param parameters.ArchivedParam
if err := c.ShouldBindQuery(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong parameters")
return utils.ErrorBadRequest.Wrap(err, "wrong parameters")
}
result, err := h.embeddingModelBL.GetAll(c.Request.Context(), identity.User, &param)
if err != nil {
Expand All @@ -65,7 +65,7 @@ func (h *EmbeddingModelHandler) GetAll(c *gin.Context, identity *security.Identi
func (h *EmbeddingModelHandler) GetByID(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
embeddingModel, err := h.embeddingModelBL.GetByID(c.Request.Context(), identity.User, id)
if err != nil {
Expand All @@ -87,7 +87,7 @@ func (h *EmbeddingModelHandler) GetByID(c *gin.Context, identity *security.Ident
func (h *EmbeddingModelHandler) Create(c *gin.Context, identity *security.Identity) error {
var param parameters.EmbeddingModelParam
if err := c.ShouldBind(&param); err != nil {
return utils.InvalidInput.New("invalid params")
return utils.ErrorBadRequest.New("invalid params")
}
embeddingModel, err := h.embeddingModelBL.Create(c.Request.Context(), identity.User, &param)
if err != nil {
Expand All @@ -110,11 +110,11 @@ func (h *EmbeddingModelHandler) Create(c *gin.Context, identity *security.Identi
func (h *EmbeddingModelHandler) Update(c *gin.Context, identity *security.Identity) error {
var param parameters.EmbeddingModelParam
if err := c.ShouldBind(&param); err != nil {
return utils.InvalidInput.New("invalid params")
return utils.ErrorBadRequest.New("invalid params")
}
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
embeddingModel, err := h.embeddingModelBL.Update(c.Request.Context(), identity.User, id, &param)
if err != nil {
Expand All @@ -137,11 +137,11 @@ func (h *EmbeddingModelHandler) Update(c *gin.Context, identity *security.Identi
func (h *EmbeddingModelHandler) Delete(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
action := c.Param("action")
if !(action == ActionRestore || action == ActionDelete) {
return utils.InvalidInput.Newf("invalid action: should be %s or %s", ActionRestore, ActionDelete)
return utils.ErrorBadRequest.Newf("invalid action: should be %s or %s", ActionRestore, ActionDelete)
}
var embedingModel *model.EmbeddingModel
switch action {
Expand Down
18 changes: 9 additions & 9 deletions backend/api/handler/persona.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (h *PersonaHandler) Mount(route *gin.Engine, authMiddleware gin.HandlerFunc
func (h *PersonaHandler) GetAll(c *gin.Context, identity *security.Identity) error {
var param parameters.ArchivedParam
if err := c.BindQuery(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong parameters")
return utils.ErrorBadRequest.Wrap(err, "wrong parameters")
}
personas, err := h.personaBL.GetAll(c.Request.Context(), identity.User, param.Archived)
if err != nil {
Expand All @@ -64,7 +64,7 @@ func (h *PersonaHandler) GetAll(c *gin.Context, identity *security.Identity) err
func (h *PersonaHandler) GetByID(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
persona, err := h.personaBL.GetByID(c.Request.Context(), identity.User, id)
if err != nil {
Expand All @@ -85,8 +85,8 @@ func (h *PersonaHandler) GetByID(c *gin.Context, identity *security.Identity) er
// @Router /manage/personas [post]
func (h *PersonaHandler) Create(c *gin.Context, identity *security.Identity) error {
var param parameters.PersonaParam
if err := c.ShouldBindJSON(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong payload")
if err := server.BindJsonAndValidate(c, &param); err != nil {
return err
}
persona, err := h.personaBL.Create(c.Request.Context(), identity.User, &param)
if err != nil {
Expand All @@ -109,11 +109,11 @@ func (h *PersonaHandler) Create(c *gin.Context, identity *security.Identity) err
func (h *PersonaHandler) Update(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
var param parameters.PersonaParam
if err = c.ShouldBindJSON(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong payload")
if err := server.BindJsonAndValidate(c, &param); err != nil {
return err
}
persona, err := h.personaBL.Update(c.Request.Context(), id, identity.User, &param)
if err != nil {
Expand All @@ -136,11 +136,11 @@ func (h *PersonaHandler) Update(c *gin.Context, identity *security.Identity) err
func (h *PersonaHandler) Archive(c *gin.Context, identity *security.Identity) error {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id == 0 {
return utils.InvalidInput.New("id should be presented")
return utils.ErrorBadRequest.New("id should be presented")
}
action := c.Param("action")
if !(action == ActionRestore || action == ActionDelete) {
return utils.InvalidInput.Newf("invalid action: should be %s or %s", ActionRestore, ActionDelete)
return utils.ErrorBadRequest.Newf("invalid action: should be %s or %s", ActionRestore, ActionDelete)
}
persona, err := h.personaBL.Archive(c.Request.Context(), identity.User, id, action == ActionRestore)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions backend/api/handler/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func (h *TenantHandler) AddUser(c *gin.Context, identity *security.Identity) err

var param parameters.AddUserParam
if err := c.ShouldBind(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong parameter")
return utils.ErrorBadRequest.Wrap(err, "wrong parameter")
}
if err := param.Validate(); err != nil {
return utils.InvalidInput.New(err.Error())
return utils.ErrorBadRequest.New(err.Error())
}
user, err := h.tenantBL.AddUser(c.Request.Context(), identity.User, param.Email, param.Role)
if err != nil {
Expand Down Expand Up @@ -100,10 +100,10 @@ func (h *TenantHandler) EditUser(c *gin.Context, identity *security.Identity) er
}
var param parameters.EditUserParam
if err := c.ShouldBind(&param); err != nil {
return utils.InvalidInput.Wrap(err, "wrong parameter")
return utils.ErrorBadRequest.Wrap(err, "wrong parameter")
}
if err := param.Validate(); err != nil {
return utils.InvalidInput.New(err.Error())
return utils.ErrorBadRequest.New(err.Error())
}
user, err := h.tenantBL.UpdateUser(c.Request.Context(), identity.User, userID, param.Role)
if err != nil {
Expand Down
Loading

0 comments on commit 1ae52c7

Please sign in to comment.