mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-13 11:35:26 +00:00
Update threads to use model/store (#97)
This commit is contained in:
47
threads/handler/update_thread.go
Normal file
47
threads/handler/update_thread.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/micro/micro/v3/service/auth"
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
"github.com/micro/services/pkg/model"
|
||||
pb "github.com/micro/services/threads/proto"
|
||||
)
|
||||
|
||||
// Update a threads topic
|
||||
func (s *Threads) UpdateThread(ctx context.Context, req *pb.UpdateThreadRequest, rsp *pb.UpdateThreadResponse) error {
|
||||
_, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||
}
|
||||
// validate the request
|
||||
if len(req.Id) == 0 {
|
||||
return ErrMissingID
|
||||
}
|
||||
if len(req.Topic) == 0 {
|
||||
return ErrMissingTopic
|
||||
}
|
||||
|
||||
t := &Thread{ID: req.Id}
|
||||
|
||||
if err := model.Read(ctx, t); err == model.ErrNotFound {
|
||||
return ErrNotFound
|
||||
} else if err != nil {
|
||||
logger.Errorf("Error reading thread: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// update the thread
|
||||
t.Topic = req.Topic
|
||||
if err := model.Update(ctx, t); err != nil {
|
||||
logger.Errorf("Error updating thread: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// serialize the result
|
||||
rsp.Thread = t.Serialize()
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user