Skip to content

Commit

Permalink
feat(integration): 增加 Integration API 和相关功能
Browse files Browse the repository at this point in the history
- 在 init.go 中初始化 IntegrationApi 变量,并在初始化函数中创建 IntegrationService。
- 新增 integration.go 文件,定义了 IntegrationApiImpl 结构体及其方法,包括 GetIntegration CreateIntegration UpdateIntegration 和 DeleteIntegration。
- 在 template.go 文件中,为现有的模板 API 方法增加了 SuccessMessage 函数调用。
- 新增 dao/integration.go 文件,定义了 IntegrationDB 结构体及其方法,包括 GetIntegrationByName CreateIntegration UpdateIntegrationByName 和 DeleteIntegrationByName。
- 在 entity/error.go 文件中,添加了与 Integration 相关的错误码和错误消息常量。
- 新增 entity/integration.go 文件,定义了与 Integration 相关的请求和响应结构体。
- 在 model/integration.go 文件中,为 Integration 模型的 Name 字段添加了 unique 约束。
- 更新 service/global.go 文件,在初始化数据库时创建 integrationDatabase 对象。
- 新增 service/integration.go 文件,定义了 IntegrationService 结构体及其方法,包括 GetIntegration CreateIntegration UpdateIntegration 和 DeleteIntegration。
- 在 router/integration.go 文件中,定义了 IntegrationApiRouterGroup 变量,包含了与 Integration 相关的路由配置。
- 在 router/router.go 文件中,添加了 IntegrationApiRouterGroup 到路由引擎中。
- 更新 go.mod 和 go.sum 文件,升级了 github.com/alioth-center/infrastructure 依赖到 v1.2.18 版本。
  • Loading branch information
sunist-c committed Aug 8, 2024
1 parent 82726dd commit 373730c
Show file tree
Hide file tree
Showing 25 changed files with 780 additions and 123 deletions.
1 change: 1 addition & 0 deletions app/api/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"encoding/json"

"github.com/alioth-center/foreseen/app/service"
"github.com/alioth-center/infrastructure/network/http"
)
Expand Down
1 change: 1 addition & 0 deletions app/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ func init() {
NotifyApi = NotifyApiImpl{srv: service.NewNotifyService()}
CallbackApi = CallbackApiImpl{srv: service.NewCallbackService()}
TemplateApi = TemplateApiImpl{srv: service.NewTemplateService()}
IntegrationApi = IntegrationApiImpl{srv: service.NewIntegrationService()}
}
45 changes: 45 additions & 0 deletions app/api/integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package api

import (
"github.com/alioth-center/foreseen/app/entity"
"github.com/alioth-center/foreseen/app/service"
"github.com/alioth-center/infrastructure/network/http"
)

var IntegrationApi IntegrationApiImpl

type IntegrationApiImpl struct {
srv *service.IntegrationService
}

func (impl IntegrationApiImpl) GetIntegration() http.Chain[*entity.GetIntegrationRequest, *entity.GetIntegrationResponse] {
return http.NewChain[*entity.GetIntegrationRequest, *entity.GetIntegrationResponse](
service.CheckToken[*entity.GetIntegrationRequest],
service.SuccessMessage[*entity.GetIntegrationRequest],
impl.srv.GetIntegration,
)
}

func (impl IntegrationApiImpl) CreateIntegration() http.Chain[*entity.CreateIntegrationRequest, *entity.CreateIntegrationResponse] {
return http.NewChain[*entity.CreateIntegrationRequest, *entity.CreateIntegrationResponse](
service.CheckToken[*entity.CreateIntegrationRequest],
service.SuccessMessage[*entity.CreateIntegrationRequest],
impl.srv.CreateIntegration,
)
}

func (impl IntegrationApiImpl) UpdateIntegration() http.Chain[*entity.UpdateIntegrationRequest, *entity.UpdateIntegrationResponse] {
return http.NewChain[*entity.UpdateIntegrationRequest, *entity.UpdateIntegrationResponse](
service.CheckToken[*entity.UpdateIntegrationRequest],
service.SuccessMessage[*entity.UpdateIntegrationRequest],
impl.srv.UpdateIntegration,
)
}

func (impl IntegrationApiImpl) DeleteIntegration() http.Chain[*entity.DeleteIntegrationRequest, *entity.DeleteIntegrationResponse] {
return http.NewChain[*entity.DeleteIntegrationRequest, *entity.DeleteIntegrationResponse](
service.CheckToken[*entity.DeleteIntegrationRequest],
service.SuccessMessage[*entity.DeleteIntegrationRequest],
impl.srv.DeleteIntegration,
)
}
19 changes: 19 additions & 0 deletions app/api/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,39 @@ type TemplateApiImpl struct {
func (impl TemplateApiImpl) GetTemplate() http.Chain[*entity.GetTemplateRequest, *entity.GetTemplateResponse] {
return http.NewChain[*entity.GetTemplateRequest, *entity.GetTemplateResponse](
service.CheckToken[*entity.GetTemplateRequest],
service.SuccessMessage[*entity.GetTemplateRequest],
impl.srv.GetTemplate,
)
}

func (impl TemplateApiImpl) CreateTemplate() http.Chain[*entity.CreateTemplateRequest, *entity.CreateTemplateResponse] {
return http.NewChain[*entity.CreateTemplateRequest, *entity.CreateTemplateResponse](
service.CheckToken[*entity.CreateTemplateRequest],
service.SuccessMessage[*entity.CreateTemplateRequest],
impl.srv.CreateTemplate,
)
}

func (impl TemplateApiImpl) UpdateTemplate() http.Chain[*entity.UpdateTemplateRequest, *entity.UpdateTemplateResponse] {
return http.NewChain[*entity.UpdateTemplateRequest, *entity.UpdateTemplateResponse](
service.CheckToken[*entity.UpdateTemplateRequest],
service.SuccessMessage[*entity.UpdateTemplateRequest],
impl.srv.UpdateTemplate,
)
}

func (impl TemplateApiImpl) DeleteTemplate() http.Chain[*entity.DeleteTemplateRequest, *entity.DeleteTemplateResponse] {
return http.NewChain[*entity.DeleteTemplateRequest, *entity.DeleteTemplateResponse](
service.CheckToken[*entity.DeleteTemplateRequest],
service.SuccessMessage[*entity.DeleteTemplateRequest],
impl.srv.DeleteTemplate,
)
}

func (impl TemplateApiImpl) GetTemplatePreview() http.Chain[*entity.GetTemplatePreviewRequest, *entity.GetTemplatePreviewResponse] {
return http.NewChain[*entity.GetTemplatePreviewRequest, *entity.GetTemplatePreviewResponse](
service.CheckToken[*entity.GetTemplatePreviewRequest],
service.SuccessMessage[*entity.GetTemplatePreviewRequest],
impl.srv.GetTemplatePreview,
)
}
34 changes: 34 additions & 0 deletions app/dao/integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dao

import (
"context"

"github.com/alioth-center/foreseen/app/model"
"github.com/alioth-center/infrastructure/database"
)

type IntegrationDB struct {
core database.DatabaseV2
}

func NewIntegrationDB(core database.DatabaseV2) *IntegrationDB {
return &IntegrationDB{core: core}
}

func (db *IntegrationDB) GetIntegrationByName(ctx context.Context, name string) (integration *model.Integration, err error) {
integration = new(model.Integration)
return integration, db.core.GetDataBySingleCondition(ctx, integration, model.IntegrationCols.Name, name)
}

func (db *IntegrationDB) CreateIntegration(ctx context.Context, integration *model.Integration) (created bool, err error) {
return db.core.CreateSingleDataIfNotExist(ctx, integration)
}

func (db *IntegrationDB) UpdateIntegrationByName(ctx context.Context, name string, updates *model.Integration) (err error) {
return db.core.UpdateDataBySingleCondition(ctx, updates, model.IntegrationCols.Name, name)
}

func (db *IntegrationDB) DeleteIntegrationByName(ctx context.Context, name string) (deleted bool, err error) {
sess := db.core.GetGormCore(ctx).Delete(&model.Integration{}, &model.Integration{Name: name})
return sess.RowsAffected > 0, sess.Error
}
24 changes: 16 additions & 8 deletions app/dao/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ package dao

import (
"context"

"github.com/alioth-center/foreseen/app/model"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/alioth-center/infrastructure/database"
)

type TemplateDB struct {
core *gorm.DB
core database.DatabaseV2
}

func NewTemplateDB(core *gorm.DB) *TemplateDB {
func NewTemplateDB(core database.DatabaseV2) *TemplateDB {
return &TemplateDB{core: core}
}

func (db *TemplateDB) GetTemplateByName(ctx context.Context, name string) (template *model.Template, err error) {
template = &model.Template{}
return template, db.core.WithContext(ctx).Model(&model.Template{}).Where(model.TemplateCols.Name, name).Scan(template).Error
template = new(model.Template)
return template, db.core.GetDataBySingleCondition(ctx, template, model.TemplateCols.Name, name)
}

func (db *TemplateDB) CreateTemplate(ctx context.Context, template *model.Template) (created bool, err error) {
session := db.core.WithContext(ctx).Model(&model.Template{}).Clauses(clause.OnConflict{DoNothing: true}).Create(template)
return session.RowsAffected > 0, session.Error
return db.core.CreateSingleDataIfNotExist(ctx, template)
}

func (db *TemplateDB) UpdateTemplateByName(ctx context.Context, name string, updates *model.Template) (err error) {
return db.core.UpdateDataBySingleCondition(ctx, updates, model.TemplateCols.Name, name)
}

func (db *TemplateDB) DeleteTemplateByName(ctx context.Context, name string) (deleted bool, err error) {
sess := db.core.GetGormCore(ctx).Delete(&model.Template{}, &model.Template{Name: name})
return sess.RowsAffected > 0, sess.Error
}
4 changes: 1 addition & 3 deletions app/entity/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"github.com/alioth-center/infrastructure/config"
)

var (
GlobalConfig Config
)
var GlobalConfig Config

func init() {
err := config.LoadConfig(&GlobalConfig, "config/config.yaml")
Expand Down
28 changes: 24 additions & 4 deletions app/entity/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@ const (
ErrorCodeAuthorizationError = 4011
ErrorCodeDatabaseError = 5011

ErrorCodeGetTemplateNotExist = 4041
ErrorCodeCreateTemplateConflict = 4091
ErrorCodeGetTemplateNotExist = 4041
ErrorCodeCreateTemplateArgumentsError = 4001
ErrorCodeCreateTemplateConflict = 4091
ErrorCodeUpdateTemplateNotExist = 4041
ErrorCodeDeleteTemplateNotExist = 4041
ErrorCodeGetTemplatePreviewNotExist = 4041

ErrorCodeGetIntegrationNotExist = 4041
ErrorCodeCreateIntegrationTooMuchSecrets = 4001
ErrorCodeCreateIntegrationConflict = 4091
ErrorCodeUpdateIntegrationTooMuchSecrets = 4001
ErrorCodeDeleteIntegrationNotExist = 4041
)

const (
ErrorMessageAuthorizationError = "invalid authorization"
ErrorMessageDatabaseError = "database error"

ErrorMessageGetTemplateNotExist = "template not exist"
ErrorMessageCreateTemplateConflict = "template already exist"
ErrorMessageGetTemplateNotExist = "template not exist"
ErrorMessageCreateTemplateArgumentsError = "invalid arguments"
ErrorMessageCreateTemplateConflict = "template already exist"
ErrorMessageUpdateTemplateNotExist = "template not exist"
ErrorMessageDeleteTemplateNotExist = "template not exist"
ErrorMessageGetTemplatePreviewNotExist = "template not exist"

ErrorMessageGetIntegrationNotExist = "integration not exist"
ErrorMessageCreateIntegrationTooMuchSecrets = "too much secrets"
ErrorMessageCreateIntegrationConflict = "integration already exist"
ErrorMessageUpdateIntegrationTooMuchSecrets = "too much secrets"
ErrorMessageDeleteIntegrationNotExist = "integration not exist"
)
41 changes: 41 additions & 0 deletions app/entity/integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package entity

import "github.com/alioth-center/infrastructure/network/http"

type GetIntegrationRequest http.NoBody

type GetIntegrationResult struct {
IntegrationID int `json:"integration_id"`
IntegrationName string `json:"integration_name"`
}

type GetIntegrationResponse = BaseResponse

type CreateIntegrationRequest struct {
Name string `json:"name" vc:"key:name,required"`
Secrets []string `json:"secrets" vc:"key:secrets,required"`
}

type CreateIntegrationResult struct {
IntegrationID int `json:"integration_id"`
IntegrationName string `json:"integration_name"`
}

type CreateIntegrationResponse = BaseResponse

type UpdateIntegrationRequest struct {
Secrets []string `json:"secrets" vc:"key:secrets,required"`
}

type UpdateIntegrationResult struct {
IntegrationID int `json:"integration_id"`
IntegrationName string `json:"integration_name"`
}

type UpdateIntegrationResponse = BaseResponse

type DeleteIntegrationRequest http.NoBody

type DeleteIntegrationResult http.NoResponse

type DeleteIntegrationResponse = BaseResponse
22 changes: 22 additions & 0 deletions app/entity/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package entity

import (
"encoding/json"

"github.com/alioth-center/infrastructure/network/http"
)

Expand Down Expand Up @@ -31,6 +32,27 @@ type CreateTemplateResult struct {

type CreateTemplateResponse = BaseResponse

type UpdateTemplateRequest struct {
Content string `json:"content" vc:"key:content,required"`
Arguments json.RawMessage `json:"arguments,omitempty" vc:"key:arguments"`
}

type UpdateTemplateResult struct {
TemplateID int `json:"template_id"`
Name string `json:"name"`
Content string `json:"content"`
Arguments json.RawMessage `json:"arguments"`
Preview string `json:"preview"`
}

type UpdateTemplateResponse = BaseResponse

type DeleteTemplateRequest = http.NoBody

type DeleteTemplateResult = http.NoResponse

type DeleteTemplateResponse = BaseResponse

type GetTemplatePreviewRequest = json.RawMessage

type GetTemplatePreviewResult struct {
Expand Down
2 changes: 1 addition & 1 deletion app/model/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package model

type Client struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
Name string `gorm:"column:name;type:varchar(255);notnull;uniqueIndex:idx_name"`
Name string `gorm:"column:name;type:varchar(255);notnull;unique;uniqueIndex:idx_name"`
Secret string `gorm:"column:secret;type:varchar(255);notnull;uniqueIndex:idx_secret"`
CreatedAt int64 `gorm:"column:created_at;autoCreateTime"`
UpdatedAt int64 `gorm:"column:updated_at;autoUpdateTime"`
Expand Down
2 changes: 1 addition & 1 deletion app/model/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package model

type Integration struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
Name string `gorm:"column:name;type:varchar(255);notnull;uniqueIndex:idx_name"`
Name string `gorm:"column:name;type:varchar(255);notnull;unique;uniqueIndex:idx_name"`
Secret1 string `gorm:"column:secret1;type:varchar(255)"`
Secret2 string `gorm:"column:secret2;type:varchar(255)"`
Secret3 string `gorm:"column:secret3;type:varchar(255)"`
Expand Down
2 changes: 1 addition & 1 deletion app/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package model
type User struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
Type int32 `gorm:"column:type;type:integer;notnull;default:0;index:idx_type"`
Name string `gorm:"column:name;type:varchar(255);notnull;uniqueIndex:idx_name"`
Name string `gorm:"column:name;type:varchar(255);notnull;unique;uniqueIndex:idx_name"`
CreatedAt int64 `gorm:"column:created_at;autoCreateTime"`
UpdatedAt int64 `gorm:"column:updated_at;autoUpdateTime"`
}
Expand Down
1 change: 1 addition & 0 deletions app/service/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"encoding/json"

"github.com/alioth-center/foreseen/app/entity"
"github.com/alioth-center/infrastructure/logger"
"github.com/alioth-center/infrastructure/network/http"
Expand Down
Loading

0 comments on commit 373730c

Please sign in to comment.