Closes #27 Reviewed-on: phoenix/textsender-api#31 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
201 lines
5.9 KiB
Go
201 lines
5.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/google/uuid"
|
|
|
|
"git.kundeng.us/phoenix/textsender-api/internal/store"
|
|
"git.kundeng.us/phoenix/textsender-models/pkg/message/scheduling"
|
|
)
|
|
|
|
type RequestAddScheduledMessageEvent struct {
|
|
RecipientId uuid.UUID `json:"recipient_id"`
|
|
MessageId uuid.UUID `json:"message_id"`
|
|
ScheduledMessageId uuid.UUID `json:"scheduled_message_id"`
|
|
}
|
|
|
|
type AddScheduledMessageEventResponse struct {
|
|
Message string `json:"message"`
|
|
Data []scheduling.ScheduledMessageEvent `json:"data"`
|
|
}
|
|
|
|
type GetScheduledMessageEventResponse struct {
|
|
Message string `json:"message"`
|
|
Data []scheduling.ScheduledMessageEvent `json:"data"`
|
|
}
|
|
|
|
type DeleteScheduledMessageEventResponse struct {
|
|
Message string `json:"message"`
|
|
Data []scheduling.ScheduledMessageEvent `json:"data"`
|
|
}
|
|
|
|
type ScheduledMessageEventHandler struct {
|
|
ScheduledMessageEventStore store.ScheduledMessageEventStore
|
|
ScheduledMessageStore store.ScheduledMessageStore
|
|
}
|
|
|
|
func NewScheduledMessageEventHandler(str store.ScheduledMessageEventStore, schStore store.ScheduledMessageStore) *ScheduledMessageEventHandler {
|
|
return &ScheduledMessageEventHandler{ScheduledMessageEventStore: str, ScheduledMessageStore: schStore}
|
|
}
|
|
|
|
func (s *ScheduledMessageEventHandler) AddScheduledMessageEvent(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var req RequestAddScheduledMessageEvent
|
|
if err := ExtractFromRequest(r, &req); err != nil {
|
|
http.Error(w, "Invalid JSON: "+err.Error(), http.StatusBadRequest)
|
|
}
|
|
defer r.Body.Close()
|
|
|
|
event := scheduling.ScheduledMessageEvent{RecipientId: req.RecipientId, MessageId: req.MessageId, ScheduledMessageId: req.ScheduledMessageId}
|
|
|
|
var statusCode int
|
|
var resp AddScheduledMessageEventResponse
|
|
|
|
if event.RecipientId == uuid.Nil {
|
|
statusCode = http.StatusBadRequest
|
|
resp.Message = "Recipient Id is nil"
|
|
} else if event.MessageId == uuid.Nil {
|
|
statusCode = http.StatusBadRequest
|
|
resp.Message = "Message Id is nil"
|
|
} else if event.ScheduledMessageId == uuid.Nil {
|
|
statusCode = http.StatusBadRequest
|
|
resp.Message = "Scheduled Message Id is nil"
|
|
} else {
|
|
ctx := r.Context()
|
|
|
|
if schMsg, err := s.ScheduledMessageStore.Get(ctx, event.ScheduledMessageId); err != nil {
|
|
statusCode = http.StatusInternalServerError
|
|
resp.Message = err.Error()
|
|
} else {
|
|
if schMsg.Status == scheduling.Pending {
|
|
if exists, err := s.ScheduledMessageEventStore.Exists(ctx, &event); err == nil {
|
|
if exists {
|
|
statusCode = http.StatusBadRequest
|
|
resp.Message = "Event already present"
|
|
} else {
|
|
if err = s.ScheduledMessageEventStore.CreateScheduledMessageEvent(ctx, &event); err == nil {
|
|
statusCode = http.StatusCreated
|
|
resp.Message = "Successful"
|
|
resp.Data = append(resp.Data, event)
|
|
} else {
|
|
statusCode = http.StatusInternalServerError
|
|
resp.Message = err.Error()
|
|
}
|
|
}
|
|
} else {
|
|
statusCode = http.StatusInternalServerError
|
|
resp.Message = err.Error()
|
|
}
|
|
} else {
|
|
statusCode = http.StatusBadRequest
|
|
resp.Message = "Scheduled message cannot be modified due to the status"
|
|
}
|
|
}
|
|
}
|
|
|
|
RespondWithJSON(w, statusCode, &resp)
|
|
}
|
|
|
|
func (s *ScheduledMessageEventHandler) GetScheduledMessageEvent(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Metnot allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
id := chi.URLParam(r, "id")
|
|
if len(id) == 0 {
|
|
pathParts := strings.Split(r.URL.Path, "/")
|
|
if len(pathParts) < 7 {
|
|
http.Error(w, "Id not provided", http.StatusBadRequest)
|
|
return
|
|
} else {
|
|
id = pathParts[6]
|
|
}
|
|
}
|
|
|
|
var statusCode int
|
|
var resp GetScheduledMessageEventResponse
|
|
|
|
if parsedId, err := uuid.Parse(id); err != nil {
|
|
resp.Message = err.Error()
|
|
statusCode = http.StatusBadRequest
|
|
} else {
|
|
ctx := r.Context()
|
|
if event, err := s.ScheduledMessageEventStore.Get(ctx, parsedId); err != nil {
|
|
resp.Message = err.Error()
|
|
statusCode = http.StatusInternalServerError
|
|
} else {
|
|
if event != nil {
|
|
resp.Message = "Successful"
|
|
statusCode = http.StatusOK
|
|
resp.Data = append(resp.Data, *event)
|
|
} else {
|
|
statusCode = http.StatusNotFound
|
|
resp.Message = "Scheduled message event not found"
|
|
}
|
|
}
|
|
}
|
|
|
|
RespondWithJSON(w, statusCode, &resp)
|
|
}
|
|
|
|
func (s *ScheduledMessageEventHandler) DeleteScheduledMessageEvent(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodDelete {
|
|
http.Error(w, "Metnot allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
id := chi.URLParam(r, "id")
|
|
if len(id) == 0 {
|
|
pathParts := strings.Split(r.URL.Path, "/")
|
|
if len(pathParts) < 7 {
|
|
http.Error(w, "Id not provided", http.StatusBadRequest)
|
|
return
|
|
} else {
|
|
id = pathParts[6]
|
|
}
|
|
}
|
|
|
|
var resp DeleteScheduledMessageEventResponse
|
|
var statusCode int
|
|
|
|
if parsedId, err := uuid.Parse(id); err != nil {
|
|
resp.Message = err.Error()
|
|
statusCode = http.StatusBadRequest
|
|
} else {
|
|
ctx := r.Context()
|
|
if event, err := s.ScheduledMessageEventStore.Get(ctx, parsedId); err != nil {
|
|
resp.Message = err.Error()
|
|
statusCode = http.StatusInternalServerError
|
|
} else {
|
|
if schMsg, err := s.ScheduledMessageStore.Get(ctx, event.ScheduledMessageId); err != nil {
|
|
resp.Message = err.Error()
|
|
statusCode = http.StatusInternalServerError
|
|
} else {
|
|
if schMsg.Status == scheduling.Pending {
|
|
if err = s.ScheduledMessageEventStore.Delete(ctx, event.Id); err != nil {
|
|
resp.Message = err.Error()
|
|
statusCode = http.StatusInternalServerError
|
|
} else {
|
|
resp.Message = "Successful"
|
|
resp.Data = append(resp.Data, *event)
|
|
statusCode = http.StatusOK
|
|
}
|
|
} else {
|
|
statusCode = http.StatusBadRequest
|
|
resp.Message = "Invalid status"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
RespondWithJSON(w, statusCode, &resp)
|
|
}
|