-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from IgorPolyakov/main
New project struct, init work on frontend & add user and team pages
- Loading branch information
Showing
50 changed files
with
3,096 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package api | ||
|
||
import ( | ||
"ctf01d/internal/app/models" | ||
"ctf01d/internal/app/repository" | ||
"ctf01d/internal/app/view" | ||
"database/sql" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
type RequestTeam struct { | ||
Name string `json:"name"` | ||
SocialLinks string `json:"social_links"` | ||
Description string `json:"description"` | ||
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()}) | ||
return | ||
} | ||
|
||
teamRepo := repository.NewTeamRepository(db) | ||
newTeam := &models.Team{ | ||
Name: team.Name, | ||
SocialLinks: team.SocialLinks, | ||
Description: team.Description, | ||
UniversityId: team.UniversityId, | ||
} | ||
if err := teamRepo.Create(r.Context(), newTeam); err != nil { | ||
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"}) | ||
} | ||
|
||
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"}) | ||
return | ||
} | ||
respondWithJSON(w, http.StatusOK, map[string]string{"data": "Team deleted successfully"}) | ||
} | ||
|
||
func GetTeamByIdHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
id := vars["id"] | ||
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"}) | ||
return | ||
} | ||
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()}) | ||
return | ||
} | ||
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"}) | ||
return | ||
} | ||
teamRepo := repository.NewTeamRepository(db) | ||
updateTeam := &models.Team{ | ||
Name: team.Name, | ||
SocialLinks: team.SocialLinks, | ||
Description: team.Description, | ||
UniversityId: team.UniversityId, | ||
} | ||
vars := mux.Vars(r) | ||
id := vars["id"] | ||
updateTeam.Id = id | ||
if err := teamRepo.Update(r.Context(), updateTeam); err != nil { | ||
respondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) | ||
return | ||
} | ||
respondWithJSON(w, http.StatusOK, map[string]string{"data": "Team updated successfully"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package api | ||
|
||
import ( | ||
"ctf01d/internal/app/models" | ||
"ctf01d/internal/app/repository" | ||
"ctf01d/internal/app/view" | ||
"database/sql" | ||
"net/http" | ||
) | ||
|
||
func ListUniversitiesHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) { | ||
queryParam := r.URL.Query().Get("term") | ||
|
||
universityRepo := repository.NewUniversityRepository(db) | ||
var universities []*models.University | ||
var err error | ||
|
||
if queryParam != "" { | ||
universities, err = universityRepo.Search(r.Context(), queryParam) | ||
} else { | ||
universities, err = universityRepo.List(r.Context()) | ||
} | ||
|
||
if err != nil { | ||
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()}) | ||
return | ||
} | ||
respondWithJSON(w, http.StatusOK, view.NewUniversitiesFromModels(universities)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package models | ||
|
||
type Team struct { | ||
Id string `db:"id"` | ||
Name string `db:"name"` | ||
Description string `db:"description"` | ||
UniversityId string `db:"university_id"` | ||
SocialLinks string `db:"social_links"` | ||
AvatarUrl string `db:"avatar_url"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package models | ||
|
||
type University struct { | ||
Id string `db:"id"` | ||
Name string `db:"name"` | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.