feat: impl requests handllers for contacts and healthcheck
This commit is contained in:
135
internal/handler/contact_handler.go
Normal file
135
internal/handler/contact_handler.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"gitea.gabilandia.com/gabdlr/agenda-web-go/internal/models"
|
||||
"gitea.gabilandia.com/gabdlr/agenda-web-go/internal/repository"
|
||||
)
|
||||
|
||||
type ContactHandler struct {
|
||||
repository repository.Repository[models.Contact]
|
||||
}
|
||||
|
||||
const required_field = "is required"
|
||||
const invalid_id = "Invalid ID"
|
||||
|
||||
func HandleContacts(mux *http.ServeMux, repo repository.Repository[models.Contact]) {
|
||||
ch := &ContactHandler{repository: repo}
|
||||
routes := []Route{
|
||||
{"GET /contacts", ch.getAll},
|
||||
{"GET /contacts/{id}", ch.getByID},
|
||||
{"POST /contacts", ch.create},
|
||||
{"PUT /contacts/{id}", ch.update},
|
||||
{"DELETE /contacts/{id}", ch.delete},
|
||||
}
|
||||
NewBaseHandler(mux, routes)
|
||||
}
|
||||
|
||||
func (h *ContactHandler) getAll(w http.ResponseWriter, r *http.Request) {
|
||||
contacts, err := h.repository.GetAll()
|
||||
if err != nil {
|
||||
InternalError(w, err)
|
||||
return
|
||||
}
|
||||
JSONSuccess(w, contacts, "Contact list retrieved successfully", http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *ContactHandler) getByID(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
BadRequest(w, ErrInvalidFormat, invalid_id)
|
||||
return
|
||||
}
|
||||
|
||||
contact, err := h.repository.GetByID(id)
|
||||
if err != nil {
|
||||
InternalError(w, err)
|
||||
return
|
||||
}
|
||||
if contact == nil {
|
||||
NotFound(w, ErrNotFound, "Contact not found")
|
||||
return
|
||||
}
|
||||
JSONSuccess(w, contact, "Contact retrieved successfully", http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *ContactHandler) create(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var contact models.Contact
|
||||
if err := json.NewDecoder(r.Body).Decode(&contact); err != nil {
|
||||
BadRequest(w, ErrInvalidJSON, "Error parsing the contact")
|
||||
return
|
||||
}
|
||||
|
||||
if contact.Name == "" {
|
||||
BadRequest(w, ErrMissingRequired, RequiredFieldErr("name"))
|
||||
return
|
||||
}
|
||||
|
||||
if contact.Company == "" {
|
||||
BadRequest(w, ErrMissingRequired, RequiredFieldErr("company"))
|
||||
return
|
||||
}
|
||||
|
||||
if contact.Phone == "" {
|
||||
BadRequest(w, ErrMissingRequired, RequiredFieldErr("phone"))
|
||||
return
|
||||
}
|
||||
|
||||
id, err := h.repository.Create(&contact)
|
||||
if err != nil {
|
||||
InternalError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
contact.ID = int(id)
|
||||
JSONSuccess(w, contact, "Contact created successfully", http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h *ContactHandler) update(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
if err != nil {
|
||||
BadRequest(w, ErrInvalidFormat, invalid_id)
|
||||
return
|
||||
}
|
||||
|
||||
var contact models.Contact
|
||||
if err := json.NewDecoder(r.Body).Decode(&contact); err != nil {
|
||||
BadRequest(w, ErrInvalidJSON, "Error parsing the contact")
|
||||
return
|
||||
}
|
||||
|
||||
contact.ID = id
|
||||
|
||||
updateErr := h.repository.Update(&contact)
|
||||
|
||||
if updateErr != nil {
|
||||
InternalError(w, updateErr)
|
||||
return
|
||||
}
|
||||
|
||||
JSONSuccess(w, nil, "Contact updated successfully", http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *ContactHandler) delete(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.PathValue("id"))
|
||||
|
||||
if err != nil {
|
||||
BadRequest(w, ErrInvalidFormat, invalid_id)
|
||||
return
|
||||
}
|
||||
rowsAffected, err := h.repository.Delete(id)
|
||||
|
||||
if err != nil {
|
||||
InternalError(w, err)
|
||||
return
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
BadRequest(w, ErrNotFound, "Contact not found")
|
||||
return
|
||||
}
|
||||
JSONSuccess(w, nil, "Contact deleted successfully", http.StatusOK)
|
||||
}
|
||||
Reference in New Issue
Block a user