mirror of
https://github.com/sigmasternchen/threadule
synced 2025-03-15 08:09:01 +00:00
feat: added updateThread method to backend
This commit is contained in:
parent
91f6ab1b7b
commit
606d4bae3d
8 changed files with 83 additions and 6 deletions
|
@ -1,6 +1,9 @@
|
|||
package app
|
||||
|
||||
import "threadule/backend/internal/data/models"
|
||||
import (
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"threadule/backend/internal/data/models"
|
||||
)
|
||||
|
||||
type Data interface {
|
||||
CountUsers() (int64, error)
|
||||
|
@ -22,9 +25,11 @@ type Data interface {
|
|||
UpdateAccount(account *models.Account) error
|
||||
|
||||
AddThread(thread *models.Thread) error
|
||||
UpdateThread(thread *models.Thread) error
|
||||
GetThread(id uuid.UUID, user *models.User) (*models.Thread, error)
|
||||
GetScheduledThreads() ([]models.Thread, error)
|
||||
GetTweetsForThread(thread *models.Thread) ([]models.Tweet, error)
|
||||
UpdateThread(thread *models.Thread) error
|
||||
UpdateThreadWithoutTweets(thread *models.Thread) error
|
||||
|
||||
UpdateTweet(tweet *models.Tweet) error
|
||||
}
|
||||
|
|
|
@ -14,4 +14,5 @@ type Logic interface {
|
|||
AddAccountResolve(user *models.User, id string, pin string) (*models.Account, error)
|
||||
|
||||
AddThread(thread *models.Thread, user *models.User) error
|
||||
UpdateThread(thread *models.Thread, user *models.User) error
|
||||
}
|
||||
|
|
|
@ -1,18 +1,36 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"gorm.io/gorm/clause"
|
||||
"threadule/backend/internal/data/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (d *Data) UpdateThread(thread *models.Thread) error {
|
||||
return d.db.
|
||||
Omit("Account").
|
||||
Save(thread).
|
||||
Error
|
||||
}
|
||||
|
||||
func (d *Data) UpdateThreadWithoutTweets(thread *models.Thread) error {
|
||||
return d.db.
|
||||
Omit(clause.Associations).
|
||||
Save(thread).
|
||||
Error
|
||||
}
|
||||
|
||||
func (d *Data) GetThread(id uuid.UUID, user *models.User) (*models.Thread, error) {
|
||||
var thread models.Thread
|
||||
err := d.db.
|
||||
Joins("Account").
|
||||
Where("Account.user_id = ?", user.ID).
|
||||
First(&thread, id).
|
||||
Error
|
||||
return &thread, err
|
||||
}
|
||||
|
||||
func (d *Data) GetTweetsForThread(thread *models.Thread) ([]models.Tweet, error) {
|
||||
var tweets []models.Tweet
|
||||
err := d.db.
|
||||
|
|
|
@ -21,6 +21,27 @@ func (l *Logic) AddThread(thread *models.Thread, user *models.User) error {
|
|||
return ErrNotFound
|
||||
}
|
||||
|
||||
thread.Status = models.ThreadScheduled
|
||||
for i := range thread.Tweets {
|
||||
thread.Tweets[i].Status = models.TweetScheduled
|
||||
}
|
||||
|
||||
err = l.ctx.Data.AddThread(thread)
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *Logic) UpdateThread(thread *models.Thread, user *models.User) error {
|
||||
oldThread, err := l.ctx.Data.GetThread(thread.ID, user)
|
||||
if err != nil {
|
||||
return ErrNotFound
|
||||
}
|
||||
thread.AccountID = oldThread.AccountID
|
||||
|
||||
thread.Status = models.ThreadScheduled
|
||||
for i := range thread.Tweets {
|
||||
thread.Tweets[i].Status = models.TweetScheduled
|
||||
}
|
||||
|
||||
err = l.ctx.Data.UpdateThread(thread)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ func (l *Logic) sendThread(thread *models.Thread) {
|
|||
client := l.getAcountClient(thread.Account)
|
||||
|
||||
thread.Status = models.ThreadProcessing
|
||||
err := l.ctx.Data.UpdateThread(thread)
|
||||
err := l.ctx.Data.UpdateThreadWithoutTweets(thread)
|
||||
l.ctx.Log.Errorf("couldn't update thread in DB: %v", err)
|
||||
|
||||
tweets, err := l.ctx.Data.GetTweetsForThread(thread)
|
||||
|
@ -66,7 +66,7 @@ func (l *Logic) sendThread(thread *models.Thread) {
|
|||
*errorString = err.Error()
|
||||
thread.Status = models.ThreadFailed
|
||||
thread.Error = errorString
|
||||
err = l.ctx.Data.UpdateThread(thread)
|
||||
err = l.ctx.Data.UpdateThreadWithoutTweets(thread)
|
||||
l.ctx.Log.Errorf("couldn't update thread in DB: %v", err)
|
||||
|
||||
return
|
||||
|
@ -100,14 +100,14 @@ func (l *Logic) sendThread(thread *models.Thread) {
|
|||
thread.Error = errorString
|
||||
}
|
||||
|
||||
err = l.ctx.Data.UpdateThread(thread)
|
||||
err = l.ctx.Data.UpdateThreadWithoutTweets(thread)
|
||||
l.ctx.Log.Errorf("couldn't update thread in DB: %v", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
thread.Status = models.ThreadDone
|
||||
err = l.ctx.Data.UpdateThread(thread)
|
||||
err = l.ctx.Data.UpdateThreadWithoutTweets(thread)
|
||||
l.ctx.Log.Errorf("couldn't update thread in DB: %v", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package presentation
|
||||
|
||||
import (
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"threadule/backend/internal/data/models"
|
||||
"threadule/backend/internal/web"
|
||||
)
|
||||
|
@ -25,3 +26,29 @@ func AddThread(ctx *web.Context) {
|
|||
return
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateThread(ctx *web.Context) {
|
||||
var thread models.Thread
|
||||
err := ctx.ReadJSON(&thread)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
thread.ID, err = uuid.FromString(ctx.Params.ByName("id"))
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, err)
|
||||
}
|
||||
|
||||
err = ctx.AppCtx.Logic.UpdateThread(&thread, ctx.Session.User)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = ctx.WriteJSON(&thread)
|
||||
if err != nil {
|
||||
ErrorResponse(ctx, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,6 +37,10 @@ func (r *router) POST(path string, handler web.Handler) {
|
|||
r.Router.POST(path, ctxWrapper(r.appCtx, handler))
|
||||
}
|
||||
|
||||
func (r *router) PUT(path string, handler web.Handler) {
|
||||
r.Router.PUT(path, ctxWrapper(r.appCtx, handler))
|
||||
}
|
||||
|
||||
func (r *router) OPTIONS(path string, handler web.Handler) {
|
||||
r.Router.OPTIONS(path, ctxWrapper(r.appCtx, handler))
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ func Setup(ctx *app.Context) http.Handler {
|
|||
router.POST("/account/:id", authenticated(AddAccountResolve))
|
||||
|
||||
router.POST("/thread/", authenticated(AddThread))
|
||||
router.PUT("/thread/:id", authenticated(UpdateThread))
|
||||
|
||||
return router
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue