81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"tutor/internal/interview"
|
|
)
|
|
|
|
type createDiagnosticSessionRequest struct {
|
|
UserID string `json:"user_id"`
|
|
TargetRole string `json:"target_role"`
|
|
Stack []string `json:"stack"`
|
|
InterviewTimeline string `json:"interview_timeline"`
|
|
}
|
|
|
|
type submitDiagnosticAnswerRequest struct {
|
|
QuestionID string `json:"question_id"`
|
|
AnswerText string `json:"answer_text"`
|
|
}
|
|
|
|
func (h Handler) createDiagnosticSession(w http.ResponseWriter, r *http.Request) {
|
|
var req createDiagnosticSessionRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
|
return
|
|
}
|
|
|
|
session, err := h.diagnostic.CreateSession(r.Context(), interview.CreateSessionInput{
|
|
UserID: req.UserID,
|
|
TargetRole: req.TargetRole,
|
|
Stack: req.Stack,
|
|
InterviewTimeline: req.InterviewTimeline,
|
|
})
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, session)
|
|
}
|
|
|
|
func (h Handler) getDiagnosticSession(w http.ResponseWriter, r *http.Request) {
|
|
session, err := h.diagnostic.GetSession(r.PathValue("id"))
|
|
if errors.Is(err, interview.ErrSessionNotFound) {
|
|
writeError(w, http.StatusNotFound, "diagnostic session not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "could not load diagnostic session")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, session)
|
|
}
|
|
|
|
func (h Handler) submitDiagnosticAnswer(w http.ResponseWriter, r *http.Request) {
|
|
var req submitDiagnosticAnswerRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
|
return
|
|
}
|
|
|
|
answer, err := h.diagnostic.SubmitAnswer(r.Context(), interview.SubmitAnswerInput{
|
|
SessionID: r.PathValue("id"),
|
|
QuestionID: req.QuestionID,
|
|
AnswerText: req.AnswerText,
|
|
})
|
|
if errors.Is(err, interview.ErrSessionNotFound) || errors.Is(err, interview.ErrQuestionNotFound) {
|
|
writeError(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, answer)
|
|
}
|