Skip to content

Commit

Permalink
Merge pull request #52 from Killerrekt/master
Browse files Browse the repository at this point in the history
changes
  • Loading branch information
aditansh authored Mar 17, 2024
2 parents f5177a0 + a0b4200 commit 3f4e326
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 19 deletions.
34 changes: 34 additions & 0 deletions internal/controllers/admin_ideas_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

services "github.com/CodeChefVIT/devsoc-backend-24/internal/services/idea"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
)

Expand All @@ -23,3 +24,36 @@ func GetAllIdeas(ctx echo.Context) error {
"status": "true",
})
}

func ShortList(ctx echo.Context) error {
var req struct {
TeamID uuid.UUID `json:"team_id"`
}

if err := ctx.Bind(&req); err != nil {
return ctx.JSON(http.StatusBadRequest, map[string]string{
"message": "send better json",
"status": "fail",
})
}

if req.TeamID == uuid.Nil {
return ctx.JSON(http.StatusBadRequest, map[string]string{
"message": "send better json",
"status": "fail",
})
}

err := services.ShortlistIdea(req.TeamID)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": "db go brrrr : " + err.Error(),
"status": "error",
})
}

return ctx.JSON(http.StatusAccepted, map[string]string{
"message": "the team id has been selected",
"status": "success",
})
}
9 changes: 9 additions & 0 deletions internal/controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ func CompleteProfile(ctx echo.Context) error {

err := services.InsertVITDetials(user.ID, vitInfo)
if err != nil {
var pgerr *pgconn.PgError
if errors.As(err, &pgerr) {
if pgerr.Code == "23505" {
return ctx.JSON(http.StatusExpectationFailed, map[string]string{
"message": "team name already exists",
"status": "failed to update team",
})
}
}
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": err.Error(),
"status": "error",
Expand Down
7 changes: 6 additions & 1 deletion internal/models/idea_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Idea struct {
Github string `json:"github_link"`
Figma string `json:"figma_link"`
Others string `json:"others"`
IsSelected bool `json:"-"`
IsSelected bool `json:"is_selected"`
}

type IdeaRequest struct {
Expand All @@ -34,3 +34,8 @@ type UpdateIdeaRequest struct {
type SelectIdeaRequest struct {
IdeaID uuid.UUID `json:"idea_id" validate:"required"`
}

type AdminGetIdea struct {
Idea
TeamID uuid.UUID `json:"team_id"`
}
5 changes: 5 additions & 0 deletions internal/models/project_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ type UpdateProjectRequest struct {
FigmaLink string `json:"figma_link" validate:"omitempty,url"`
Others string `json:"others"`
}

type AdminGetProject struct {
Project
TeamID uuid.UUID `json:"team_id"`
}
11 changes: 11 additions & 0 deletions internal/models/team_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ type GetTeam struct {
Ideas Idea `json:"idea"`
Project Project `json:"project"`
}

type AdminGetTeam struct {
ID uuid.UUID `json:"id"`
TeamName string `json:"team_name"`
TeamCode string `json:"team_code"`
LeaderID uuid.UUID `json:"leader_id"`
Round int `json:"round"`
Users []AdminGetTeamUser `json:"users"`
Ideas Idea `json:"idea"`
Project Project `json:"project"`
}
5 changes: 5 additions & 0 deletions internal/models/user_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ type ResetPasswordRequest struct {
Password string `json:"new_password" validate:"required,min=6"`
}

type AdminGetTeamUser struct {
GetUser
Email string `json:"email"`
}

func NewUser(email string, password string, role string) *User {
return &User{
ID: uuid.New(),
Expand Down
6 changes: 4 additions & 2 deletions internal/routes/admin_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ func AdminRoutes(incomingRoutes *echo.Echo) {
admin.GET("/team/project/:id", controllers.GetProjectByTeamID)
admin.GET("/team/leader/:id", controllers.GetTeamLeader)
admin.GET("/team/idea/:id", controllers.GetIdeaByTeamID)
admin.GET("/team/ban/:id", controllers.BanTeam, middleware.EditOnly)
admin.GET("/team/unban/:id", controllers.UnbanTeam, middleware.EditOnly)
admin.POST("/team/ban/:id", controllers.BanTeam, middleware.EditOnly)
admin.POST("/team/unban/:id", controllers.UnbanTeam, middleware.EditOnly)

admin.GET("/projects/all", controllers.GetAllProject)
admin.GET("/ideas/all", controllers.GetAllIdeas)

admin.POST("/idea/shortlist", controllers.ShortList)

//admin.GET("/team/freshers", controllers.GetAllFresherTeams)
//admin.GET("/team/females", controllers.GetAllFemaleTeams)
}
16 changes: 9 additions & 7 deletions internal/services/idea/get_idea.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,34 @@ import (
)

func GetIdeaByTeamID(teamid uuid.UUID) (models.Idea, error) {
query := "SELECT title, description, track, github, figma, others FROM ideas WHERE teamid = $1"
query := "SELECT title, description, track, github, figma, others, is_selected FROM ideas WHERE teamid = $1"

var idea models.Idea

err := database.DB.QueryRow(query, teamid).Scan(&idea.Title, &idea.Description,
&idea.Track, &idea.Github, &idea.Figma, &idea.Others)
&idea.Track, &idea.Github, &idea.Figma, &idea.Others, &idea.IsSelected)

return idea, err
}

func GetAllIdeas() ([]models.Idea, error) {
query := "SELECT id, title, description, track, github, figma, others FROM ideas"
func GetAllIdeas() ([]models.AdminGetIdea, error) {
query := "SELECT id, title, description, track, github, figma, others, teamid FROM ideas"

rows, err := database.DB.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()

var ideas []models.Idea
var ideas []models.AdminGetIdea
for rows.Next() {
var idea models.Idea
var idea models.AdminGetIdea
var temp string
if err := rows.Scan(&idea.ID, &idea.Title, &idea.Description, &idea.Track,
&idea.Github, &idea.Figma, &idea.Others); err != nil {
&idea.Github, &idea.Figma, &idea.Others, &temp); err != nil {
return nil, err
}
idea.TeamID = uuid.MustParse(temp)
ideas = append(ideas, idea)
}
if err := rows.Err(); err != nil {
Expand Down
40 changes: 40 additions & 0 deletions internal/services/idea/shortlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package services

import (
"context"
"database/sql"

"github.com/CodeChefVIT/devsoc-backend-24/internal/database"
"github.com/google/uuid"
)

func ShortlistIdea(team_id uuid.UUID) error {

tx, err := database.DB.BeginTx(
context.Background(),
&sql.TxOptions{Isolation: sql.LevelSerializable},
)
if err != nil {
return err
}

_, err = tx.Exec(`UPDATE teams SET round = 1 WHERE id = $1`, team_id)
if err != nil {
tx.Rollback()
return err
}

_, err = tx.Exec(`UPDATE ideas SET is_selected = true WHERE teamid = $1`, team_id)
if err != nil {
tx.Rollback()
return err
}

err = tx.Commit()
if err != nil {
tx.Rollback()
return err
}

return nil
}
13 changes: 8 additions & 5 deletions internal/services/projects/get_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,26 @@ func GetProjectByID(projectID uuid.UUID) (models.Project, error) {
return proj, nil
}

func GetAllProjects() ([]models.Project, error) {
query := `SELECT name, description, github, figma, track, others FROM projects`
func GetAllProjects() ([]models.AdminGetProject, error) {
query := `SELECT name, description, github, figma, track, others, teamid FROM projects`

rows, err := database.DB.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()

var projects []models.Project
var projects []models.AdminGetProject

for rows.Next() {
var proj models.Project
var proj models.AdminGetProject
var temp string
err := rows.Scan(
&proj.Name, &proj.Description, &proj.GithubLink, &proj.FigmaLink,
&proj.Track, &proj.Others,
&proj.Track, &proj.Others, &temp,
)
proj.TeamID = uuid.MustParse(temp)

if err != nil {
return nil, err
}
Expand Down
101 changes: 97 additions & 4 deletions internal/services/team/get_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package services

import (
"database/sql"
"fmt"
"strconv"

"github.com/google/uuid"
Expand All @@ -10,12 +11,103 @@ import (
"github.com/CodeChefVIT/devsoc-backend-24/internal/models"
)

func GetAllTeams() ([]models.GetTeam, error) {
func GetAllTeams() ([]models.AdminGetTeam, error) {
var teams []models.AdminGetTeam

query := `SELECT teams.id, teams.name, teams.code, teams.leader_id, teams.round,
users.first_name, users.last_name, users.reg_no, users.id, users.is_leader, users.email,
ideas.title, ideas.description, ideas.track, ideas.github, ideas.figma, ideas.others ,
projects.name, projects.description, projects.github, projects.figma, projects.track, projects.others
FROM teams
INNER JOIN users ON users.team_id = teams.id
LEFT JOIN projects ON teams.id = projects.teamid
LEFT JOIN ideas ON teams.id = ideas.teamid`

rows, err := database.DB.Query(query)
if err != nil {
return teams, err
}
defer rows.Close()

columns, err := rows.Columns()
if err != nil {
return teams, err
}

var team_details map[uuid.UUID]models.AdminGetTeam = make(map[uuid.UUID]models.AdminGetTeam)

for rows.Next() {
var temp models.AdminGetTeam
values := make([]sql.NullString, len(columns))
columnPointers := make([]interface{}, len(columns))

for i := range values {
columnPointers[i] = &values[i]
}

if err := rows.Scan(columnPointers...); err != nil {
return teams, err
}

id := uuid.MustParse(values[0].String)
check := team_details[id]

var temp_user models.AdminGetTeamUser
temp_user.FullName = values[5].String + values[6].String
temp_user.RegNo = values[7].String
temp_user.ID = uuid.MustParse(values[8].String)
temp_user.IsLeader, _ = strconv.ParseBool(values[9].String)
temp_user.Email = values[10].String

fmt.Println(check.ID)

if check.ID == uuid.Nil {
temp.ID = uuid.MustParse(values[0].String)
temp.TeamName = values[1].String
temp.TeamCode = values[2].String
temp.LeaderID = uuid.MustParse(values[3].String)
temp.Round, _ = strconv.Atoi(values[4].String)
temp.Users = append(temp.Users, temp_user)
if values[11].Valid {
temp.Ideas = models.Idea{
Title: values[11].String,
Description: values[12].String,
Track: values[13].String,
Github: values[14].String,
Figma: values[15].String,
Others: values[16].String,
}
}

if values[17].Valid {
temp.Project = models.Project{
Name: values[17].String,
Description: values[18].String,
GithubLink: values[19].String,
FigmaLink: values[20].String,
Track: values[21].String,
Others: values[22].String,
}
}
team_details[temp.ID] = temp
} else {
check.Users = append(check.Users, temp_user)
team_details[check.ID] = check
}
}
for _, team := range team_details {
teams = append(teams, team)
}
//fmt.Println(teams)
return teams, nil
}

/*func GetAllTeams() ([]models.GetTeam, error) {
var teams []models.GetTeam
query := `SELECT teams.name,teams.code, teams.leader_id, teams.round ,
users.first_name, users.last_name, users.id, users.reg_no,
ideas.title, ideas.description, ideas.track, ideas.github, ideas.figma, ideas.others ,
users.first_name, users.last_name, users.id, users.reg_no, users,
ideas.title, ideas.description, ideas.track, ideas.github, ideas.figma, ideas.others ,
projects.name, projects.description, projects.github, projects.figma, projects.track, projects.others
FROM teams
INNER JOIN users ON users.team_id = teams.id
Expand Down Expand Up @@ -80,6 +172,7 @@ func GetAllTeams() ([]models.GetTeam, error) {
isLeader = false
}
user := models.GetUser{
FullName: values[4].String,
RegNo: values[5].String,
Expand All @@ -92,7 +185,7 @@ func GetAllTeams() ([]models.GetTeam, error) {
}
return teams, nil
}
}*/

func FindTeamByTeamID(team_id uuid.UUID) (models.GetTeam, error) {
var team models.GetTeam
Expand Down

0 comments on commit 3f4e326

Please sign in to comment.