Skip to content

Commit

Permalink
Merge pull request #6 from IgorPolyakov/main
Browse files Browse the repository at this point in the history
add game details page & user connect with the team
  • Loading branch information
IgorPolyakov authored Apr 20, 2024
2 parents 2cdfcaa + b6b485f commit 32d79f1
Show file tree
Hide file tree
Showing 28 changed files with 2,776 additions and 2,412 deletions.
30 changes: 30 additions & 0 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,36 @@ paths:
responses:
'204':
description: Service deleted successfully
/api/universities:
get:
summary: Retrieves a list of universities
description: |
This endpoint retrieves universities. It can optionally filter universities that match a specific term.
parameters:
- in: query
name: term
schema:
type: string
description: Optional search term to filter universities by name.
required: false
responses:
'200':
description: A JSON array of universities
content:
application/json:
schema:
type: array
items:
type: object
properties:
id:
type: integer
description: The unique identifier of the university
example: 1
name:
type: string
description: The name of the university
example: "Анапский филиал Кубанского государственного аграрного университета"
components:
schemas:
UserRequest:
Expand Down
60 changes: 26 additions & 34 deletions internal/app/api/games.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package api
import (
"ctf01d/internal/app/models"
"ctf01d/internal/app/repository"
api_helpers "ctf01d/internal/app/utils"
"ctf01d/internal/app/view"
"database/sql"
"encoding/json"
"log"
"net/http"
"strconv"
"time"

"github.com/gorilla/mux"
Expand All @@ -22,11 +23,11 @@ type RequestGame struct {
func CreateGameHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
var game RequestGame
if err := json.NewDecoder(r.Body).Decode(&game); err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload: " + err.Error()})
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload: " + err.Error()})
return
}
if game.EndTime.Before(game.StartTime) {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "EndTime must be after StartTime"})
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "EndTime must be after StartTime"})
return
}
gameRepo := repository.NewGameRepository(db)
Expand All @@ -36,50 +37,54 @@ func CreateGameHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
Description: game.Description,
}
if err := gameRepo.Create(r.Context(), newGame); err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create game: " + err.Error()})
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create game: " + err.Error()})
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"data": "Game created successfully"})
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Game created successfully"})
}

func DeleteGameHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
gameRepo := repository.NewGameRepository(db)
if err := gameRepo.Delete(r.Context(), id); err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to delete game"})
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to delete game"})
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"data": "Game deleted successfully"})
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Game deleted successfully"})
}

func GetGameByIdHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
id, err := strconv.Atoi(vars["id"])
if err != nil {
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
gameRepo := repository.NewGameRepository(db)
game, err := gameRepo.GetById(r.Context(), id)
game, err := gameRepo.GetGameDetails(r.Context(), id) // короткий ответ, если нужен см. GetById
if err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch game"})
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch game: " + err.Error()})
return
}
respondWithJSON(w, http.StatusOK, view.NewGameFromModel(game))
api_helpers.RespondWithJSON(w, http.StatusOK, view.NewGameDetailsFromModel(game))
}

func ListGamesHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
gameRepo := repository.NewGameRepository(db)
games, err := gameRepo.List(r.Context())
if err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
respondWithJSON(w, http.StatusOK, view.NewGamesFromModels(games))
api_helpers.RespondWithJSON(w, http.StatusOK, view.NewGamesFromModels(games))
}

func UpdateGameHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
// fixme update не проверяет есть ли запись в бд
var game RequestGame
if err := json.NewDecoder(r.Body).Decode(&game); err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
return
}
gameRepo := repository.NewGameRepository(db)
Expand All @@ -89,29 +94,16 @@ func UpdateGameHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
Description: game.Description,
}
vars := mux.Vars(r)
id := vars["id"]
updateGame.Id = id
err := gameRepo.Update(r.Context(), updateGame)
if err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
id, err2 := strconv.Atoi(vars["id"])
if err2 != nil {
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": err2.Error()})
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"data": "Game updated successfully"})
}

// fixme это общий респондер - надо его вынести отсюда
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
w.Header().Set("Content-Type", "application/json")
response, err := json.Marshal(payload)
updateGame.Id = id
err := gameRepo.Update(r.Context(), updateGame)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
if _, err := w.Write([]byte(`{"error": "Error marshaling the response object"}`)); err != nil {
log.Printf("Error writing error response: %v", err)
}
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
w.WriteHeader(code)
if _, err := w.Write(response); err != nil {
log.Printf("Error writing response: %v", err)
}
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Game updated successfully"})
}
36 changes: 23 additions & 13 deletions internal/app/api/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package api
import (
"ctf01d/internal/app/models"
"ctf01d/internal/app/repository"
api_helpers "ctf01d/internal/app/utils"
"ctf01d/internal/app/view"
"database/sql"
"encoding/json"
"net/http"
"strconv"

"github.com/gorilla/mux"
)
Expand All @@ -15,39 +17,42 @@ type RequestTeam struct {
Name string `json:"name"`
SocialLinks string `json:"social_links"`
Description string `json:"description"`
AvatarUrl string `json:"avatar_url"`
UniversityId string `json:"university_id"`
}

func CreateTeamHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
var team RequestTeam
if err := json.NewDecoder(r.Body).Decode(&team); err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload: " + err.Error()})
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload: " + err.Error()})
return
}

teamRepo := repository.NewTeamRepository(db)
// fixme request to model надо вынести и переиспользовать
newTeam := &models.Team{
Name: team.Name,
SocialLinks: team.SocialLinks,
Description: team.Description,
UniversityId: team.UniversityId,
AvatarUrl: api_helpers.PrepareImage(team.AvatarUrl),
}
if err := teamRepo.Create(r.Context(), newTeam); err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create team: " + err.Error()})
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to create team: " + err.Error()})
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"data": "Team created successfully"})
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Team created successfully"})
}

func DeleteTeamHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
teamRepo := repository.NewTeamRepository(db)
if err := teamRepo.Delete(r.Context(), id); err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to delete team"})
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to delete team"})
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"data": "Team deleted successfully"})
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Team deleted successfully"})
}

func GetTeamByIdHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
Expand All @@ -56,26 +61,26 @@ func GetTeamByIdHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
teamRepo := repository.NewTeamRepository(db)
team, err := teamRepo.GetById(r.Context(), id)
if err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch team"})
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to fetch team"})
return
}
respondWithJSON(w, http.StatusOK, view.NewTeamFromModel(team))
api_helpers.RespondWithJSON(w, http.StatusOK, view.NewTeamFromModel(team))
}

func ListTeamsHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
teamRepo := repository.NewTeamRepository(db)
teams, err := teamRepo.List(r.Context())
if err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
respondWithJSON(w, http.StatusOK, view.NewTeamsFromModels(teams))
api_helpers.RespondWithJSON(w, http.StatusOK, view.NewTeamsFromModels(teams))
}

func UpdateTeamHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
var team RequestTeam
if err := json.NewDecoder(r.Body).Decode(&team); err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid request payload"})
return
}
teamRepo := repository.NewTeamRepository(db)
Expand All @@ -84,13 +89,18 @@ func UpdateTeamHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
SocialLinks: team.SocialLinks,
Description: team.Description,
UniversityId: team.UniversityId,
AvatarUrl: team.AvatarUrl,
}
vars := mux.Vars(r)
id := vars["id"]
id, err := strconv.Atoi(vars["id"])
if err != nil {
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
updateTeam.Id = id
if err := teamRepo.Update(r.Context(), updateTeam); err != nil {
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
respondWithJSON(w, http.StatusOK, map[string]string{"data": "Team updated successfully"})
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "Team updated successfully"})
}
5 changes: 3 additions & 2 deletions internal/app/api/university.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"ctf01d/internal/app/models"
"ctf01d/internal/app/repository"
api_helpers "ctf01d/internal/app/utils"
"ctf01d/internal/app/view"
"database/sql"
"net/http"
Expand All @@ -22,9 +23,9 @@ func ListUniversitiesHandler(db *sql.DB, w http.ResponseWriter, r *http.Request)
}

if err != nil {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
api_helpers.RespondWithJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
respondWithJSON(w, http.StatusOK, view.NewUniversitiesFromModels(universities))
api_helpers.RespondWithJSON(w, http.StatusOK, view.NewUniversitiesFromModels(universities))

}
Loading

0 comments on commit 32d79f1

Please sign in to comment.