127 lines
3.7 KiB
Go
127 lines
3.7 KiB
Go
package contacts
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/ultisuite/ulti-backend/internal/api/apiresponse"
|
|
"github.com/ultisuite/ulti-backend/internal/api/apivalidate"
|
|
"github.com/ultisuite/ulti-backend/internal/api/middleware"
|
|
"github.com/ultisuite/ulti-backend/internal/api/query"
|
|
"github.com/ultisuite/ulti-backend/internal/nextcloud"
|
|
"github.com/ultisuite/ulti-backend/internal/permission"
|
|
)
|
|
|
|
type Handler struct {
|
|
svc *Service
|
|
logger *slog.Logger
|
|
}
|
|
|
|
func NewHandler(nc *nextcloud.Client) *Handler {
|
|
return &Handler{
|
|
svc: NewService(nc),
|
|
logger: slog.Default().With("component", "contacts-api"),
|
|
}
|
|
}
|
|
|
|
func (h *Handler) Routes() chi.Router {
|
|
r := chi.NewRouter()
|
|
read := middleware.RequirePermission(permission.ResourceContacts, permission.LevelRead)
|
|
write := middleware.RequirePermission(permission.ResourceContacts, permission.LevelWrite)
|
|
|
|
r.With(read).Get("/books", h.ListAddressBooks)
|
|
r.With(read).Get("/books/{bookID}", h.ListContacts)
|
|
r.With(read).Get("/search", h.SearchContacts)
|
|
r.With(write).Post("/books/{bookID}", h.CreateContact)
|
|
r.With(write).Delete("/*", h.DeleteContact)
|
|
return r
|
|
}
|
|
|
|
func (h *Handler) ListAddressBooks(w http.ResponseWriter, r *http.Request) {
|
|
claims := middleware.ClaimsFromContext(r.Context())
|
|
books, err := h.svc.ListAddressBooks(r.Context(), claims.Sub)
|
|
if err != nil {
|
|
h.logger.Error("list address books", "error", err)
|
|
apivalidate.WriteInternal(w, r)
|
|
return
|
|
}
|
|
apiresponse.WriteJSON(w, http.StatusOK, map[string]any{"address_books": books})
|
|
}
|
|
|
|
func (h *Handler) ListContacts(w http.ResponseWriter, r *http.Request) {
|
|
claims := middleware.ClaimsFromContext(r.Context())
|
|
params, err := query.ParseListRequest(r)
|
|
if err != nil {
|
|
apivalidate.WriteQueryError(w, r, err)
|
|
return
|
|
}
|
|
|
|
result, err := h.svc.ListContacts(r.Context(), claims.Sub, chi.URLParam(r, "bookID"), params)
|
|
if err != nil {
|
|
h.logger.Error("list contacts", "error", err)
|
|
apivalidate.WriteInternal(w, r)
|
|
return
|
|
}
|
|
apiresponse.WriteJSON(w, http.StatusOK, result)
|
|
}
|
|
|
|
func (h *Handler) SearchContacts(w http.ResponseWriter, r *http.Request) {
|
|
claims := middleware.ClaimsFromContext(r.Context())
|
|
params, err := query.ParseListRequest(r)
|
|
if err != nil {
|
|
apivalidate.WriteQueryError(w, r, err)
|
|
return
|
|
}
|
|
|
|
bookID := r.URL.Query().Get("book_id")
|
|
if bookID == "" {
|
|
bookID = "contacts"
|
|
}
|
|
q := r.URL.Query().Get("q")
|
|
|
|
result, err := h.svc.SearchContacts(r.Context(), claims.Sub, bookID, q, params)
|
|
if err != nil {
|
|
h.logger.Error("search contacts", "error", err)
|
|
apivalidate.WriteInternal(w, r)
|
|
return
|
|
}
|
|
apiresponse.WriteJSON(w, http.StatusOK, result)
|
|
}
|
|
|
|
func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request) {
|
|
claims := middleware.ClaimsFromContext(r.Context())
|
|
|
|
var contact nextcloud.Contact
|
|
if err := apivalidate.DecodeJSON(w, r, maxRequestBody, &contact); err != nil {
|
|
return
|
|
}
|
|
if verr := validateCreateContact(&contact); verr != nil {
|
|
apivalidate.WriteValidationError(w, r, verr)
|
|
return
|
|
}
|
|
|
|
if err := h.svc.CreateContact(r.Context(), claims.Sub, chi.URLParam(r, "bookID"), &contact); err != nil {
|
|
h.logger.Error("create contact", "error", err)
|
|
apivalidate.WriteInternal(w, r)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusCreated)
|
|
}
|
|
|
|
func (h *Handler) DeleteContact(w http.ResponseWriter, r *http.Request) {
|
|
claims := middleware.ClaimsFromContext(r.Context())
|
|
contactPath := chi.URLParam(r, "*")
|
|
if verr := validateDeletePath(contactPath); verr != nil {
|
|
apivalidate.WriteValidationError(w, r, verr)
|
|
return
|
|
}
|
|
if err := h.svc.DeleteContact(r.Context(), claims.Sub, contactPath); err != nil {
|
|
h.logger.Error("delete contact", "error", err)
|
|
apivalidate.WriteInternal(w, r)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|