diff --git a/database/queries/users.sql b/database/queries/users.sql index a96f131..fcf35fa 100644 --- a/database/queries/users.sql +++ b/database/queries/users.sql @@ -168,4 +168,4 @@ UPDATE users SET reg_no = $2, updated_at = now() -WHERE email = $1; \ No newline at end of file +WHERE email = $1; diff --git a/pkg/controllers/submission.go b/pkg/controllers/submission.go index 3ed87ab..197432a 100644 --- a/pkg/controllers/submission.go +++ b/pkg/controllers/submission.go @@ -33,7 +33,6 @@ func CreateSubmission(c echo.Context) error { Success: false, Message: "Invalid request payload", }) - } if errors := utils.ValSub( @@ -55,7 +54,6 @@ func CreateSubmission(c echo.Context) error { Success: false, Message: "User or team not found", }) - } exists, err := db.Queries.Submission(ctx, teamID) @@ -181,7 +179,6 @@ func GetSubmission(c echo.Context) error { Success: false, Message: "Submission not found", }) - } log.Println("Error while getting submission from db: ", err) return c.JSON(http.StatusInternalServerError, &models.Response{ diff --git a/pkg/controllers/teams.go b/pkg/controllers/teams.go index f46d42b..e69ec48 100644 --- a/pkg/controllers/teams.go +++ b/pkg/controllers/teams.go @@ -3,6 +3,7 @@ package controllers import ( "fmt" "net/http" + "strings" "time" "github.com/CodeChefVIT/devsoc-backend-26/pkg/db" @@ -49,6 +50,13 @@ func CreateTeam(c echo.Context) error { team sqlc.Team err error ) + if len(strings.TrimSpace(req.Name)) < 5 || len(req.Name) > 12 { + return c.JSON(http.StatusBadRequest, &models.Response{ + Success: false, + Message: "team name should be in between 5 to 12 characters", + }) + } + ctx := c.Request().Context() exists, err := db.Queries.NameExists(ctx, req.Name) if err != nil { @@ -63,7 +71,7 @@ func CreateTeam(c echo.Context) error { Message: "team name already taken TwT", }) } - for attempts := 0; attempts < 5; attempts++ { + for range 5 { code, genErr := utils.GenerateTeamCode(6) if genErr != nil { return c.JSON(http.StatusInternalServerError, &models.Response{ @@ -242,7 +250,6 @@ func UpdateTeam(c echo.Context) error { ID: id, Name: name, }) - if err != nil { return c.JSON(http.StatusInternalServerError, &models.Response{ Success: false, @@ -285,7 +292,6 @@ func ToggleTeamBan(c echo.Context) error { IsBanned: newBanStatus, TotalScore: existing.TotalScore, }) - if err != nil { return c.JSON(http.StatusInternalServerError, &models.Response{ Success: false, @@ -331,6 +337,7 @@ func JoinTeam(c echo.Context) error { }) } + teamKeyStr := teamKey.String() ok, err = redis.RequestManager.SetNX( @@ -339,7 +346,6 @@ func JoinTeam(c echo.Context) error { teamKeyStr, 30*time.Minute, ).Result() - if err != nil { fmt.Printf("Rediserror: %v\n", err) return c.JSON(http.StatusInternalServerError, &models.Response{ diff --git a/pkg/controllers/user.go b/pkg/controllers/user.go index 13ffc06..dac3c8f 100644 --- a/pkg/controllers/user.go +++ b/pkg/controllers/user.go @@ -131,6 +131,16 @@ func CompleteProfile(c echo.Context) error { }) } + if req.PhoneNo != "" { + existingPhone, err := db.Queries.GetUserByPhoneNo(ctx, &req.PhoneNo) + if err == nil && existingPhone.ID != user.ID { + return c.JSON(http.StatusConflict, models.Response{ + Success: false, + Message: "Phone number already in use", + }) + } + } + phone := req.PhoneNo gender := req.Gender ghub := req.GithubProfile @@ -241,6 +251,16 @@ func UpdateProfile(c echo.Context) error { }) } + if req.PhoneNo != "" { + existingPhone, err := db.Queries.GetUserByPhoneNo(ctx, &req.PhoneNo) + if err == nil && existingPhone.ID != user.ID { + return c.JSON(http.StatusConflict, models.Response{ + Success: false, + Message: "Phone number already in use", + }) + } + } + phone := req.PhoneNo gender := req.Gender ghub := req.GithubProfile diff --git a/pkg/db/sqlc/users.sql.go b/pkg/db/sqlc/users.sql.go index cb80e87..a467746 100644 --- a/pkg/db/sqlc/users.sql.go +++ b/pkg/db/sqlc/users.sql.go @@ -292,6 +292,39 @@ func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error return i, err } +const getUserByPhoneNo = `-- name: GetUserByPhoneNo :one + SELECT id, team_id, name, email, phone_no, reg_no, gender, hostel_block, room_no, github_profile, residency, role, is_leader, is_banned, is_profile_complete, google_id, google_profile_pic, created_at, updated_at +FROM users +WHERE phone_no = $1 +` + +func (q *Queries) GetUserByPhoneNo(ctx context.Context, phoneNo *string) (User, error) { + row := q.db.QueryRow(ctx, getUserByPhoneNo, phoneNo) + var i User + err := row.Scan( + &i.ID, + &i.TeamID, + &i.Name, + &i.Email, + &i.PhoneNo, + &i.RegNo, + &i.Gender, + &i.HostelBlock, + &i.RoomNo, + &i.GithubProfile, + &i.Residency, + &i.Role, + &i.IsLeader, + &i.IsBanned, + &i.IsProfileComplete, + &i.GoogleID, + &i.GoogleProfilePic, + &i.CreatedAt, + &i.UpdatedAt, + ) + return i, err +} + const getUserByRegNo = `-- name: GetUserByRegNo :one SELECT id, team_id, name, email, phone_no, reg_no, gender, hostel_block, room_no, github_profile, residency, role, is_leader, is_banned, is_profile_complete, google_id, google_profile_pic, created_at, updated_at FROM users @@ -496,6 +529,7 @@ updated AS ( AND EXISTS (SELECT 1 FROM new_leader) RETURNING id, team_id, name, email, phone_no, reg_no, gender, hostel_block, room_no, github_profile, residency, role, is_leader, is_banned, is_profile_complete, google_id, google_profile_pic, created_at, updated_at ) + SELECT id, team_id, name, email, phone_no, reg_no, gender, hostel_block, room_no, github_profile, residency, role, is_leader, is_banned, is_profile_complete, google_id, google_profile_pic, created_at, updated_at FROM updated ` diff --git a/pkg/utils/validators.go b/pkg/utils/validators.go index 1c4c570..b510e3d 100644 --- a/pkg/utils/validators.go +++ b/pkg/utils/validators.go @@ -77,6 +77,11 @@ func FormatValidationErrors(err error) []string { } func ValSub(t, d, g, f, o string) string { + chk := func(v string) bool { + u, err := url.ParseRequestURI(v) + return err == nil && (u.Scheme == "http" || u.Scheme == "https") + } + t = strings.TrimSpace(t) if t == "" { return "title is required" @@ -93,14 +98,14 @@ func ValSub(t, d, g, f, o string) string { return "description too long" } - chk := func(v string) bool { - u, err := url.ParseRequestURI(v) - return err == nil && (u.Scheme == "http" || u.Scheme == "https") + g = strings.TrimSpace(g) + if g == "" { + return "github link is required" } - - if strings.TrimSpace(g) != "" && !chk(g) { + if !chk(g) { return "invalid github url" } + if strings.TrimSpace(f) != "" && !chk(f) { return "invalid figma url" }