forked from mtusi2026/mtusi2026lab2
chore: port lab 1 solution
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
"mtusi2026-messenger/internal/middleware"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
type ChatHandler struct {
|
||||
service *service.ChatService
|
||||
}
|
||||
|
||||
func NewChatHandler(service *service.ChatService) *ChatHandler {
|
||||
return &ChatHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
var req dto.CreateChatRequest
|
||||
if !httpx.DecodeJSONBody(w, r, requestID, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if !req.Validate() {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid request body",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := h.service.Create(userID, req.Title, req.MemberIDs)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrNotChatMember):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"caller must be a member of member_ids",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case errors.Is(err, service.ErrDuplicateMember):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"member_ids contains duplicates",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case errors.Is(err, service.ErrUnknownMember):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"member_ids contains unknown user",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
dto.ErrorCodeInternalError,
|
||||
"internal server error",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusCreated, dto.ToChatResponse(chat))
|
||||
}
|
||||
|
||||
func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
chatID, err := uuid.Parse(chi.URLParam(r, "chat_id"))
|
||||
if err != nil {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid chat_id",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := h.service.Get(userID, chatID)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrChatNotFound):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusNotFound,
|
||||
dto.ErrorCodeNotFound,
|
||||
"chat not found",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
dto.ErrorCodeInternalError,
|
||||
"internal server error",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusOK, dto.ToChatResponse(chat))
|
||||
}
|
||||
|
||||
func (h *ChatHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
limit, cursor, ok := httpx.ParsePagination(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
chats, nextCursor, err := h.service.List(userID, limit, cursor)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrInvalidCursor):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid cursor",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
dto.ErrorCodeInternalError,
|
||||
"internal server error",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]dto.ChatResponse, 0, len(chats))
|
||||
for _, c := range chats {
|
||||
items = append(items, dto.ToChatResponse(c))
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusOK, dto.ChatPageResponse{
|
||||
Items: items,
|
||||
NextCursor: nextCursor,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
)
|
||||
|
||||
func WriteError(
|
||||
w http.ResponseWriter,
|
||||
status int,
|
||||
code dto.ErrorCode,
|
||||
message string,
|
||||
requestID string,
|
||||
) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
|
||||
_ = json.NewEncoder(w).Encode(dto.ErrorResponse{
|
||||
Error: dto.Error{
|
||||
Code: code,
|
||||
Message: message,
|
||||
RequestID: requestID,
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
)
|
||||
|
||||
func Health(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
_ = json.NewEncoder(w).Encode(dto.HealthResponse{
|
||||
Status: "ok",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
"mtusi2026-messenger/internal/middleware"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
type MessageHandler struct {
|
||||
service *service.MessageService
|
||||
}
|
||||
|
||||
func NewMessageHandler(service *service.MessageService) *MessageHandler {
|
||||
return &MessageHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *MessageHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
chatID, err := uuid.Parse(chi.URLParam(r, "chat_id"))
|
||||
if err != nil {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid chat_id",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.CreateMessageRequest
|
||||
if !httpx.DecodeJSONBody(w, r, requestID, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if !req.Validate() {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid request body",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
message, err := h.service.Create(userID, chatID, req.Text)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrMessageChatNotFound):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusNotFound,
|
||||
dto.ErrorCodeNotFound,
|
||||
"chat not found",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
dto.ErrorCodeInternalError,
|
||||
"internal server error",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusCreated, dto.ToMessageResponse(message))
|
||||
}
|
||||
|
||||
func (h *MessageHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
chatID, err := uuid.Parse(chi.URLParam(r, "chat_id"))
|
||||
if err != nil {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid chat_id",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
limit, cursor, ok := httpx.ParsePagination(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
messages, nextCursor, err := h.service.List(userID, chatID, limit, cursor)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrMessageChatNotFound):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusNotFound,
|
||||
dto.ErrorCodeNotFound,
|
||||
"chat not found",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case errors.Is(err, service.ErrInvalidCursor):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid cursor",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
dto.ErrorCodeInternalError,
|
||||
"internal server error",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]dto.MessageResponse, 0, len(messages))
|
||||
for _, m := range messages {
|
||||
items = append(items, dto.ToMessageResponse(m))
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusOK, dto.MessagePageResponse{
|
||||
Items: items,
|
||||
NextCursor: nextCursor,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
"mtusi2026-messenger/internal/middleware"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
type UserHandler struct {
|
||||
service *service.UserService
|
||||
}
|
||||
|
||||
func NewUserHandler(service *service.UserService) *UserHandler {
|
||||
return &UserHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
|
||||
var req dto.CreateUserRequest
|
||||
if !httpx.DecodeJSONBody(w, r, requestID, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if !req.Validate() {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid request body",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.service.Create(req.Username, req.DisplayName)
|
||||
if errors.Is(err, service.ErrUsernameTaken) {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusConflict,
|
||||
dto.ErrorCodeConflict,
|
||||
"username already taken",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
dto.ErrorCodeInternalError,
|
||||
"internal server error",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusCreated, dto.UserResponse{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
DisplayName: user.DisplayName,
|
||||
CreatedAt: user.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *UserHandler) Me(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
user, err := h.service.GetByID(userID)
|
||||
if errors.Is(err, service.ErrUserNotFound) {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusNotFound,
|
||||
dto.ErrorCodeNotFound,
|
||||
"user not found",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
dto.ErrorCodeInternalError,
|
||||
"internal server error",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusOK, dto.UserResponse{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
DisplayName: user.DisplayName,
|
||||
CreatedAt: user.CreatedAt,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user