Skip to content

Commit

Permalink
add 2 new apis for bot & app
Browse files Browse the repository at this point in the history
  • Loading branch information
lyricat committed Mar 25, 2023
1 parent c04b9aa commit 311dd6b
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type (
GetAppByAppID(ctx context.Context, appID string) (*App, error)
GetAppsByUser(ctx context.Context, userID uint64) ([]*App, error)
DeleteApp(ctx context.Context, id uint64) error
UpdateApp(ctx context.Context, id uint64, name string) error
ReplaceStore(AppStore) AppService
}
)
Expand Down
1 change: 1 addition & 0 deletions core/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ type (
GetBotsByUserID(ctx context.Context, userID uint64) ([]*Bot, error)
CreateBot(ctx context.Context, userID uint64, name, model, prompt string, temperature float32, maxTurnCount, contextTurnCount int, middlewares MiddlewareConfig, public bool) (*Bot, error)
UpdateBot(ctx context.Context, id uint64, name, model, prompt string, temperature float32, maxTurnCount, contextTurnCount int, middlewares MiddlewareConfig, public bool) error
DeleteBot(ctx context.Context, id uint64) error
ReplaceStore(store BotStore) BotService
}
)
Expand Down
55 changes: 53 additions & 2 deletions handler/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/go-chi/chi"
)

type CreateAppPayload struct {
type CreateOrUpdateAppPayload struct {
Name string `json:"name"`
}

Expand Down Expand Up @@ -59,7 +59,7 @@ func CreateApp(appz core.AppService) http.HandlerFunc {
return
}

body := &CreateAppPayload{}
body := &CreateOrUpdateAppPayload{}
if err := param.Binding(r, body); err != nil {
render.Error(w, http.StatusBadRequest, err)
return
Expand Down Expand Up @@ -88,6 +88,57 @@ func CreateApp(appz core.AppService) http.HandlerFunc {
}
}

func UpdateApp(appz core.AppService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

user, found := session.UserFrom(ctx)
if !found {
render.Error(w, http.StatusUnauthorized, core.ErrUnauthorized)
return
}

body := &CreateOrUpdateAppPayload{}
if err := param.Binding(r, body); err != nil {
render.Error(w, http.StatusBadRequest, err)
return
}

body.Name = strings.TrimSpace(body.Name)

if len(body.Name) > 128 || len(body.Name) == 0 {
render.Error(w, http.StatusBadRequest, nil)
return
}

appID := chi.URLParam(r, "appID")

// validate uuid
if _, err := uuid.Parse(appID); err != nil {
render.Error(w, http.StatusBadRequest, core.ErrAppNotFound)
return
}

app, err := appz.GetAppByAppID(ctx, appID)
if err != nil {
render.Error(w, http.StatusInternalServerError, err)
return
}

if app.UserID != user.ID {
render.Error(w, http.StatusNotFound, core.ErrAppNotFound)
return
}

if err := appz.UpdateApp(ctx, user.ID, body.Name); err != nil {
render.Error(w, http.StatusInternalServerError, err)
return
}

render.JSON(w, app)
}
}

func GetMyApps(appz core.AppService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand Down
36 changes: 36 additions & 0 deletions handler/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,39 @@ func CreateBot(botz core.BotService) http.HandlerFunc {
render.JSON(w, bot)
}
}

func DeleteBot(botz core.BotService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
user, found := session.UserFrom(ctx)
if !found {
render.Error(w, http.StatusUnauthorized, core.ErrUnauthorized)
}

botIDStr := chi.URLParam(r, "botID")
botID, _ := strconv.ParseUint(botIDStr, 10, 64)

if botID <= 0 {
render.Error(w, http.StatusBadRequest, nil)
return
}

bot, err := botz.GetBot(ctx, botID)
if err != nil {
render.Error(w, http.StatusNotFound, err)
return
}

if bot.UserID != user.ID {
render.Error(w, http.StatusNotFound, core.ErrBotNotFound)
return
}

if err := botz.DeleteBot(ctx, bot.ID); err != nil {
render.Error(w, http.StatusInternalServerError, err)
return
}

render.JSON(w, bot)
}
}
2 changes: 2 additions & 0 deletions handler/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (s Server) HandleRest() http.Handler {
r.With(auth.LoginRequired()).Post("/", bot.CreateBot(s.botz))
r.With(auth.LoginRequired()).Put("/{botID}", bot.UpdateBot(s.botz))
r.With(auth.LoginRequired()).Get("/", bot.GetMyBots(s.botz))
r.With(auth.LoginRequired()).Delete("/{botID}", bot.DeleteBot(s.botz))
})

r.With(auth.LoginRequired()).Route("/users", func(r chi.Router) {
Expand All @@ -119,6 +120,7 @@ func (s Server) HandleRest() http.Handler {
r.Get("/{appID}", app.GetApp(s.appz))
r.Post("/", app.CreateApp(s.appz))
r.Get("/", app.GetMyApps(s.appz))
r.Put("/{appID}", app.UpdateApp(s.appz))
r.Delete("/{appID}", app.DeleteApp(s.appz))
})

Expand Down
7 changes: 7 additions & 0 deletions service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ func (s *service) GetAppsByUser(ctx context.Context, userID uint64) ([]*core.App
return apps, nil
}

func (s *service) UpdateApp(ctx context.Context, id uint64, name string) error {
if err := s.apps.UpdateAppName(ctx, id, name); err != nil {
return err
}
return nil
}

func (s *service) DeleteApp(ctx context.Context, id uint64) error {
if err := s.apps.DeleteApp(ctx, id); err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions service/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,10 @@ func (s *service) UpdateBot(ctx context.Context, id uint64, name, model, prompt
s.botCache.Delete(fmt.Sprintf("user-bots-%d", bot.UserID))
return nil
}

func (s *service) DeleteBot(ctx context.Context, id uint64) error {
if err := s.bots.DeleteBot(ctx, id); err != nil {
return err
}
return nil
}

0 comments on commit 311dd6b

Please sign in to comment.