feat: added getThreads api endpoint

This commit is contained in:
overflowerror 2021-08-21 15:47:30 +02:00
parent 606d4bae3d
commit 76cfef10f2
6 changed files with 34 additions and 0 deletions

View file

@ -27,6 +27,7 @@ type Data interface {
AddThread(thread *models.Thread) error
UpdateThread(thread *models.Thread) error
GetThread(id uuid.UUID, user *models.User) (*models.Thread, error)
GetThreads(user *models.User) ([]models.Thread, error)
GetScheduledThreads() ([]models.Thread, error)
GetTweetsForThread(thread *models.Thread) ([]models.Tweet, error)
UpdateThreadWithoutTweets(thread *models.Thread) error

View file

@ -15,4 +15,5 @@ type Logic interface {
AddThread(thread *models.Thread, user *models.User) error
UpdateThread(thread *models.Thread, user *models.User) error
GetThreads(user *models.User) ([]models.Thread, error)
}

View file

@ -26,11 +26,23 @@ func (d *Data) GetThread(id uuid.UUID, user *models.User) (*models.Thread, error
err := d.db.
Joins("Account").
Where("Account.user_id = ?", user.ID).
Where("status != ?", models.ThreadDone).
First(&thread, id).
Error
return &thread, err
}
func (d *Data) GetThreads(user *models.User) ([]models.Thread, error) {
var threads []models.Thread
err := d.db.
Preload("Tweets").
Joins("Account").
Where("Account.user_id = ?", user.ID).
Find(&threads).
Error
return threads, err
}
func (d *Data) GetTweetsForThread(thread *models.Thread) ([]models.Tweet, error) {
var tweets []models.Tweet
err := d.db.

View file

@ -45,3 +45,7 @@ func (l *Logic) UpdateThread(thread *models.Thread, user *models.User) error {
err = l.ctx.Data.UpdateThread(thread)
return err
}
func (l *Logic) GetThreads(user *models.User) ([]models.Thread, error) {
return l.ctx.Data.GetThreads(user)
}

View file

@ -38,6 +38,7 @@ func UpdateThread(ctx *web.Context) {
thread.ID, err = uuid.FromString(ctx.Params.ByName("id"))
if err != nil {
ErrorResponse(ctx, err)
return
}
err = ctx.AppCtx.Logic.UpdateThread(&thread, ctx.Session.User)
@ -52,3 +53,17 @@ func UpdateThread(ctx *web.Context) {
return
}
}
func GetThreads(ctx *web.Context) {
threads, err := ctx.AppCtx.Logic.GetThreads(ctx.Session.User)
if err != nil {
ErrorResponse(ctx, err)
return
}
err = ctx.WriteJSON(&threads)
if err != nil {
ErrorResponse(ctx, err)
return
}
}

View file

@ -27,6 +27,7 @@ func Setup(ctx *app.Context) http.Handler {
router.POST("/account/", authenticated(AddAccount))
router.POST("/account/:id", authenticated(AddAccountResolve))
router.GET("/thread", authenticated(GetThreads))
router.POST("/thread/", authenticated(AddThread))
router.PUT("/thread/:id", authenticated(UpdateThread))