forked from mirrors/pronouns.cc
feat(backend): add create member report endpoint
This commit is contained in:
parent
3bb97b8274
commit
9fe6529c1b
2 changed files with 54 additions and 1 deletions
|
@ -37,6 +37,10 @@ func (s *Server) createUserReport(w http.ResponseWriter, r *http.Request) error
|
|||
return errors.Wrap(err, "getting user")
|
||||
}
|
||||
|
||||
if u.DeletedAt != nil {
|
||||
return server.APIError{Code: server.ErrUserNotFound}
|
||||
}
|
||||
|
||||
var req CreateReportRequest
|
||||
err = render.Decode(r, &req)
|
||||
if err != nil {
|
||||
|
@ -56,3 +60,52 @@ func (s *Server) createUserReport(w http.ResponseWriter, r *http.Request) error
|
|||
render.JSON(w, r, map[string]any{"created": true, "created_at": report.CreatedAt})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) createMemberReport(w http.ResponseWriter, r *http.Request) error {
|
||||
ctx := r.Context()
|
||||
claims, _ := server.ClaimsFromContext(ctx)
|
||||
|
||||
memberID, err := xid.FromString(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
return server.APIError{Code: server.ErrBadRequest, Details: "Invalid member ID"}
|
||||
}
|
||||
|
||||
m, err := s.DB.Member(ctx, memberID)
|
||||
if err != nil {
|
||||
if err == db.ErrMemberNotFound {
|
||||
return server.APIError{Code: server.ErrMemberNotFound}
|
||||
}
|
||||
|
||||
log.Errorf("getting member %v: %v", memberID, err)
|
||||
return errors.Wrap(err, "getting member")
|
||||
}
|
||||
|
||||
u, err := s.DB.User(ctx, m.UserID)
|
||||
if err != nil {
|
||||
log.Errorf("getting user %v: %v", m.UserID, err)
|
||||
return errors.Wrap(err, "getting user")
|
||||
}
|
||||
|
||||
if u.DeletedAt != nil {
|
||||
return server.APIError{Code: server.ErrMemberNotFound}
|
||||
}
|
||||
|
||||
var req CreateReportRequest
|
||||
err = render.Decode(r, &req)
|
||||
if err != nil {
|
||||
return server.APIError{Code: server.ErrBadRequest}
|
||||
}
|
||||
|
||||
if len(req.Reason) > MaxReasonLength {
|
||||
return server.APIError{Code: server.ErrBadRequest, Details: "Reason cannot exceed 2000 characters"}
|
||||
}
|
||||
|
||||
report, err := s.DB.CreateReport(ctx, claims.UserID, u.ID, &m.ID, req.Reason)
|
||||
if err != nil {
|
||||
log.Errorf("creating report for %v: %v", m.ID, err)
|
||||
return errors.Wrap(err, "creating report")
|
||||
}
|
||||
|
||||
render.JSON(w, r, map[string]any{"created": true, "created_at": report.CreatedAt})
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ func Mount(srv *server.Server, r chi.Router) {
|
|||
})
|
||||
|
||||
r.With(server.MustAuth).Post("/users/{id}/reports", server.WrapHandler(s.createUserReport))
|
||||
r.With(server.MustAuth).Post("/members/{id}/reports", nil)
|
||||
r.With(server.MustAuth).Post("/members/{id}/reports", server.WrapHandler(s.createMemberReport))
|
||||
}
|
||||
|
||||
func MustAdmin(next http.Handler) http.Handler {
|
||||
|
|
Loading…
Reference in a new issue