Assignment 4: add unit, integration, and e2e tests; refactor for testability

This commit is contained in:
angelicatesvara07-crypto
2026-10-03 19:09:40 +07:00
parent 4ac21847fc
commit 3945920abb
24 changed files with 2753 additions and 147 deletions
+69 -50
View File
@@ -9,15 +9,15 @@ import (
"5803025015/w04/internal/middleware"
"5803025015/w04/internal/models"
"5803025015/w04/internal/store"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
type SnippetHandler struct {
snippetStore *store.SnippetStore
snippetStore SnippetStore
}
func NewSnippetHandler(snippetStore *store.SnippetStore) *SnippetHandler {
func NewSnippetHandler(snippetStore SnippetStore) *SnippetHandler {
return &SnippetHandler{snippetStore: snippetStore}
}
@@ -26,6 +26,16 @@ type snippetRequest struct {
Content string `json:"content"`
}
type snippetResponse struct {
ID string `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
}
func toSnippetResponse(s *models.Snippet) snippetResponse {
return snippetResponse{ID: s.ID, Title: s.Title, Content: s.Content}
}
func (h *SnippetHandler) Snippets(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
@@ -33,7 +43,7 @@ func (h *SnippetHandler) Snippets(w http.ResponseWriter, r *http.Request) {
case http.MethodPost:
h.Create(w, r)
default:
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
}
}
@@ -44,32 +54,27 @@ func (h *SnippetHandler) SnippetByID(w http.ResponseWriter, r *http.Request) {
case http.MethodDelete:
h.Delete(w, r)
default:
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
}
}
func (h *SnippetHandler) Create(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
userID := middleware.GetUserID(r.Context())
if userID == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
writeError(w, http.StatusUnauthorized, "unauthorized")
return
}
var req snippetRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
req.Title = strings.TrimSpace(req.Title)
req.Content = strings.TrimSpace(req.Content)
if req.Title == "" || req.Content == "" {
http.Error(w, "Title and content are required", http.StatusBadRequest)
writeError(w, http.StatusBadRequest, "title and content are required")
return
}
@@ -81,85 +86,99 @@ func (h *SnippetHandler) Create(w http.ResponseWriter, r *http.Request) {
}
if err := h.snippetStore.Create(r.Context(), snippet); err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
writeError(w, http.StatusInternalServerError, "internal error")
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(snippet)
writeJSON(w, http.StatusCreated, toSnippetResponse(snippet))
}
func (h *SnippetHandler) List(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
userID := middleware.GetUserID(r.Context())
if userID == "" {
writeError(w, http.StatusUnauthorized, "unauthorized")
return
}
snippets, err := h.snippetStore.GetAll(r.Context())
snippets, err := h.snippetStore.ListByOwner(r.Context(), userID)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
writeError(w, http.StatusInternalServerError, "internal error")
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(snippets)
out := make([]snippetResponse, 0, len(snippets))
for _, s := range snippets {
out = append(out, toSnippetResponse(s))
}
writeJSON(w, http.StatusOK, out)
}
func (h *SnippetHandler) Get(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
id := r.PathValue("id")
id := extractID(r)
if id == "" {
path := strings.TrimPrefix(r.URL.Path, "/snippets/")
id = strings.TrimSuffix(path, "/")
}
if id == "" {
http.Error(w, "Bad Request", http.StatusBadRequest)
writeError(w, http.StatusBadRequest, "missing id")
return
}
snippet, err := h.snippetStore.GetByID(r.Context(), id)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
http.Error(w, "Snippet not found", http.StatusNotFound)
if errors.Is(err, store.ErrNotFound) {
writeError(w, http.StatusNotFound, "snippet not found")
return
}
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
writeError(w, http.StatusInternalServerError, "internal error")
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(snippet)
writeJSON(w, http.StatusOK, toSnippetResponse(snippet))
}
func (h *SnippetHandler) Delete(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
userID := middleware.GetUserID(r.Context())
if userID == "" {
writeError(w, http.StatusUnauthorized, "unauthorized")
return
}
id := r.PathValue("id")
id := extractID(r)
if id == "" {
path := strings.TrimPrefix(r.URL.Path, "/snippets/")
id = strings.TrimSuffix(path, "/")
writeError(w, http.StatusBadRequest, "missing id")
return
}
if id == "" {
http.Error(w, "Bad Request", http.StatusBadRequest)
snippet, err := h.snippetStore.GetByID(r.Context(), id)
if err != nil {
if errors.Is(err, store.ErrNotFound) {
writeError(w, http.StatusNotFound, "snippet not found")
return
}
writeError(w, http.StatusInternalServerError, "internal error")
return
}
if snippet.UserID != userID {
writeError(w, http.StatusForbidden, "forbidden")
return
}
if err := h.snippetStore.Delete(r.Context(), id); err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
if errors.Is(err, store.ErrNotFound) {
writeError(w, http.StatusNotFound, "snippet not found")
return
}
writeError(w, http.StatusInternalServerError, "internal error")
return
}
w.WriteHeader(http.StatusNoContent)
}
func extractID(r *http.Request) string {
if id := r.PathValue("id"); id != "" {
return id
}
path := strings.TrimPrefix(r.URL.Path, "/snippets/")
path = strings.TrimSuffix(path, "/")
return path
}