2021-04-11 09:48:15 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bitbucket.org/BlackBroker/heruvym/dal"
|
|
|
|
"bitbucket.org/BlackBroker/heruvym/jwt_adapter"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/themakers/hlog"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Heruvym struct {
|
|
|
|
logger hlog.Logger
|
|
|
|
dal *dal.DAL
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(dataAccessLayer *dal.DAL, log hlog.Logger) *Heruvym {
|
|
|
|
return &Heruvym{
|
|
|
|
logger: log.Module("Service"),
|
|
|
|
dal: dataAccessLayer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h Heruvym) Register(m *http.ServeMux) *http.ServeMux {
|
|
|
|
|
|
|
|
m.HandleFunc("/create", h.CreateTicket)
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateTicketReq struct {
|
|
|
|
Title string `json:"Title"`
|
|
|
|
Message string `json:"Message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateTicketResp struct {
|
|
|
|
Ticket string `json:"Ticket"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) CreateTicket(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer func() {
|
|
|
|
if err := r.Body.Close(); err != nil {
|
|
|
|
h.logger.Emit(ErrorClose{
|
|
|
|
Err: err,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var request CreateTicketReq
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
|
|
|
http.Error(w, "Invalid json", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Title == "" {
|
|
|
|
http.Error(w, "No Title", http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Message == "" {
|
|
|
|
http.Error(w, "No Message", http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
session := jwt_adapter.Get(ctx)
|
|
|
|
|
|
|
|
if session == nil {
|
|
|
|
http.Error(w, "No session", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ticketID, err := h.dal.CreateTicket(
|
|
|
|
ctx,
|
|
|
|
session.User,
|
|
|
|
session.ID,
|
|
|
|
request.Title,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "CannotCreateTicket", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.PutMessage(ctx,
|
|
|
|
request.Message,
|
|
|
|
session.User,
|
|
|
|
session.Session,
|
|
|
|
ticketID,
|
|
|
|
[]string{},
|
|
|
|
); err != nil {
|
|
|
|
http.Error(w, "CannotCreateMessage", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := json.Marshal(CreateTicketResp{Ticket: ticketID})
|
|
|
|
if err != nil {
|
|
|
|
h.logger.Emit(ErrorMarshal{
|
|
|
|
Err: err,
|
|
|
|
})
|
|
|
|
http.Error(w, "CannotMarshalMessage", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(response); err != nil {
|
|
|
|
h.logger.Emit(ErrorMarshal{
|
|
|
|
Err: err,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-05-01 10:05:45 +00:00
|
|
|
|
|
|
|
func (h *Heruvym) GetList(f http.Flusher, r *http.Request) {
|
|
|
|
|
|
|
|
}
|