2021-04-11 09:48:15 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bitbucket.org/BlackBroker/heruvym/dal"
|
|
|
|
"bitbucket.org/BlackBroker/heruvym/jwt_adapter"
|
2021-05-01 12:36:22 +00:00
|
|
|
"bitbucket.org/BlackBroker/heruvym/tools"
|
|
|
|
"context"
|
2021-04-11 09:48:15 +00:00
|
|
|
"encoding/json"
|
2021-05-01 12:36:22 +00:00
|
|
|
"errors"
|
2021-04-11 09:48:15 +00:00
|
|
|
"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
|
|
|
|
2021-05-01 12:36:22 +00:00
|
|
|
var _ tools.DataEmitter = Heruvym{}.GetList
|
2021-05-01 10:05:45 +00:00
|
|
|
|
2021-05-01 12:36:22 +00:00
|
|
|
func (h *Heruvym) GetList(ctx context.Context) chan interface{} {
|
|
|
|
sess := jwt_adapter.Get(ctx)
|
|
|
|
|
|
|
|
output := make(chan interface{})
|
|
|
|
|
|
|
|
if sess.User == "" {
|
|
|
|
go h.unauthorizedTickets(ctx, sess.Session, output)
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
2021-05-01 10:05:45 +00:00
|
|
|
}
|
2021-05-01 12:36:22 +00:00
|
|
|
|
|
|
|
func (h *Heruvym) unauthorizedTickets(ctx context.Context, sess string, output chan interface{}) {
|
|
|
|
defer close(output)
|
|
|
|
|
|
|
|
tickets, err := h.dal.GetTickets4Sess(ctx, sess)
|
|
|
|
if err !=nil {
|
|
|
|
output <- errors.New("no tickets for session")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ticket := range tickets {
|
|
|
|
output <- ticket
|
|
|
|
}
|
|
|
|
}
|