Skip to content

Commit

Permalink
Merge pull request #16 from Killerrekt/master
Browse files Browse the repository at this point in the history
Minor changes
  • Loading branch information
aditansh authored Mar 9, 2024
2 parents 5657b55 + b59bd98 commit 72dd24b
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 24 deletions.
1 change: 1 addition & 0 deletions db/migrations/001_users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CREATE TABLE users (
college TEXT NOT NULL,
city TEXT NOT NULL,
state TEXT NOT NULL,
country TEXT NOT NULL,
gender TEXT NOT NULL,
role TEXT NOT NULL,
is_banned BOOLEAN NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/redis/go-redis/v9 v9.4.0
golang.org/x/crypto v0.18.0
golang.org/x/oauth2 v0.15.0
golang.org/x/text v0.14.0
google.golang.org/api v0.153.0
)

Expand Down Expand Up @@ -44,7 +45,6 @@ require (
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
Expand Down
42 changes: 34 additions & 8 deletions internal/controllers/idea_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"errors"
"net/http"

"github.com/CodeChefVIT/devsoc-backend-24/internal/models"
services "github.com/CodeChefVIT/devsoc-backend-24/internal/services/idea"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgconn"
"github.com/labstack/echo/v4"

"github.com/CodeChefVIT/devsoc-backend-24/internal/models"
services "github.com/CodeChefVIT/devsoc-backend-24/internal/services/idea"
"github.com/CodeChefVIT/devsoc-backend-24/internal/utils"
)

func GetIdea(ctx echo.Context) error {
Expand Down Expand Up @@ -97,7 +95,7 @@ func CreateIdea(ctx echo.Context) error {
}

func UpdateIdea(ctx echo.Context) error {
var req models.IdeaRequest
var req models.UpdateIdeaRequest
if err := ctx.Bind(&req); err != nil {
return ctx.JSON(http.StatusBadRequest, map[string]string{
"message": "Failed to parse the data",
Expand Down Expand Up @@ -128,14 +126,42 @@ func UpdateIdea(ctx echo.Context) error {
})
}

err := services.UpdateIdea(req, user.TeamID)
curr, err := services.GetIdeaByTeamID(user.TeamID)
if err != nil {
if errors.Is(err, utils.ErrInvalidTeamID) {
if errors.Is(err, sql.ErrNoRows) {
return ctx.JSON(http.StatusNotFound, map[string]string{
"message": "idea not found",
"message": "the team has not submitted an idea",
"status": "fail",
})
}
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": "db err : " + err.Error(),
"status": "fail",
})
}

if req.Title == "" {
req.Title = curr.Title
}
if req.Description == "" {
req.Description = curr.Description
}
if req.Track == "" {
req.Track = curr.Track
}
if req.Github == "" {
req.Github = curr.Github
}
if req.Figma == "" {
req.Figma = curr.Figma
}
if req.Others == "" {
req.Others = curr.Others
}

err = services.UpdateIdea(req, user.TeamID)

if err != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": "Failed to update the idea " + err.Error(),
"status": "error",
Expand Down
34 changes: 30 additions & 4 deletions internal/controllers/project_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/CodeChefVIT/devsoc-backend-24/internal/models"
services "github.com/CodeChefVIT/devsoc-backend-24/internal/services/projects"
"github.com/CodeChefVIT/devsoc-backend-24/internal/utils"
)

func GetProject(ctx echo.Context) error {
Expand Down Expand Up @@ -102,7 +101,7 @@ func CreateProject(ctx echo.Context) error {
}

func UpdateProject(ctx echo.Context) error {
var req models.ProjectRequest
var req models.UpdateProjectRequest

if err := ctx.Bind(&req); err != nil {
return ctx.JSON(http.StatusBadRequest, map[string]string{
Expand Down Expand Up @@ -134,14 +133,41 @@ func UpdateProject(ctx echo.Context) error {
})
}

err := services.UpdateProject(req, user.TeamID)
curr, err := services.GetProject(user.TeamID)
if err != nil {
if errors.Is(err, utils.ErrInvalidTeamID) {
if errors.Is(err, sql.ErrNoRows) {
return ctx.JSON(http.StatusNotFound, map[string]string{
"message": "project not found",
"status": "fail",
})
}
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": "db error : " + err.Error(),
"status": "fail",
})
}

if req.Name == "" {
req.Name = curr.Name
}
if req.Description == "" {
req.Description = curr.Description
}
if req.Track == "" {
req.Track = curr.Track
}
if req.GithubLink == "" {
req.GithubLink = curr.GithubLink
}
if req.FigmaLink == "" {
req.FigmaLink = curr.FigmaLink
}
if req.Others == "" {
req.Others = curr.Others
}

err = services.UpdateProject(req, user.TeamID)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": "Failed to update the project" + err.Error(),
"status": "error",
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/team_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func LeaveTeam(ctx echo.Context) error {
}

if team.LeaderID == user.ID {
if err := services.DeleteTeam(user.TeamID); err != nil {
if err := services.DeleteTeam(user.TeamID, user.ID); err != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]string{
"message": err.Error(),
"status": "error",
Expand Down
2 changes: 2 additions & 0 deletions internal/controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func CompleteProfile(ctx echo.Context) error {
user.College = utils.TitleCaser.String(payload.College)
user.City = utils.TitleCaser.String(payload.City)
user.State = utils.TitleCaser.String(payload.State)
user.Country = utils.TitleCaser.String(payload.Country)
user.Gender = payload.Gender
user.IsVitian = *payload.IsVitian

Expand Down Expand Up @@ -174,6 +175,7 @@ func CompleteProfile(ctx echo.Context) error {
user.College = "Vellore Institute Of Technology"
user.City = "Vellore"
user.State = "Tamil Nadu"
user.Country = "India"
}

user.IsProfileComplete = true
Expand Down
9 changes: 9 additions & 0 deletions internal/models/idea_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ type IdeaRequest struct {
Others string `json:"others"`
}

type UpdateIdeaRequest struct {
Title string `json:"title,omitempty" validate:"omitempty,min=1,max=50"`
Description string `json:"description,omitempty" validate:"omitempty,min=50,max=200"`
Track string `json:"track,omitempty"`
Github string `json:"github_link,omitempty" validate:"omitempty,url"`
Figma string `json:"figma_link,omitempty" validate:"omitempty,url"`
Others string `json:"others,omitempty"`
}

type SelectIdeaRequest struct {
IdeaID uuid.UUID `json:"idea_id" validate:"required"`
}
13 changes: 11 additions & 2 deletions internal/models/project_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ type ProjectRequest struct {
Name string `json:"name" validate:"required,min=1,max=50"`
Description string `json:"description" validate:"required,min=50,max=200"`
Track string `json:"track" validate:"required"`
GithubLink string `json:"github_link" validate:"url"`
FigmaLink string `json:"figma_link" validate:"url"`
GithubLink string `json:"github_link" validate:"omitempty,url"`
FigmaLink string `json:"figma_link" validate:"omitempty,url"`
Others string `json:"others"`
}

type UpdateProjectRequest struct {
Name string `json:"name" validate:"omitempty,min=1,max=50"`
Description string `json:"description" validate:"omitempty,min=50,max=200"`
Track string `json:"track"`
GithubLink string `json:"github_link" validate:"omitempty,url"`
FigmaLink string `json:"figma_link" validate:"omitempty,url"`
Others string `json:"others"`
}
2 changes: 2 additions & 0 deletions internal/models/user_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type User struct {
College string `json:"college"`
City string `json:"city"`
State string `json:"state"`
Country string `json:"country"`
Gender string `json:"gender"`
Role string `json:"role"`
IsBanned bool `json:"-"`
Expand Down Expand Up @@ -47,6 +48,7 @@ type CompleteUserRequest struct {
College string `json:"college"`
City string `json:"city"`
State string `json:"state"`
Country string `json:"country"`
RegNo string `json:"reg_no" validate:"required"`
}

Expand Down
2 changes: 1 addition & 1 deletion internal/services/idea/update_idea.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/google/uuid"
)

func UpdateIdea(data models.IdeaRequest, teamid uuid.UUID) error {
func UpdateIdea(data models.UpdateIdeaRequest, teamid uuid.UUID) error {
query := `UPDATE ideas SET title = $1, description = $2, track = $3, github = $4, figma = $5, others = $6 WHERE teamid = $7`
tx, _ := database.DB.BeginTx(
context.Background(),
Expand Down
2 changes: 1 addition & 1 deletion internal/services/projects/update_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/google/uuid"
)

func UpdateProject(data models.ProjectRequest, teamid uuid.UUID) error {
func UpdateProject(data models.UpdateProjectRequest, teamid uuid.UUID) error {
query := `UPDATE projects SET name = $1, description = $2, github = $3, figma = $4, track = $5, others = $6 WHERE teamid = $7`
tx, _ := database.DB.BeginTx(
context.Background(),
Expand Down
10 changes: 8 additions & 2 deletions internal/services/team/delete_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ import (
"github.com/google/uuid"
)

func DeleteTeam(id uuid.UUID) error {
func DeleteTeam(teamid uuid.UUID, userid uuid.UUID) error {

tx, _ := database.DB.BeginTx(context.TODO(), &sql.TxOptions{Isolation: sql.LevelSerializable})
_, err := tx.Exec("DELETE FROM teams WHERE id = $1", id)
_, err := tx.Exec("DELETE FROM teams WHERE id = $1", teamid)
if err != nil {
tx.Rollback()
return err
}

_, err = tx.Exec("UPDATE users SET is_leader = false WHERE id = $1", userid)
if err != nil {
tx.Rollback()
return err
Expand Down
3 changes: 2 additions & 1 deletion internal/services/user/create_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func InsertUser(user *models.User) error {
_, err := database.DB.Exec(
"INSERT INTO users (id, first_name, last_name, reg_no, email, password, gender, phone, role, college, is_added, is_banned, is_vitian, is_verified, is_profile_complete, is_leader, city, state) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)",
"INSERT INTO users (id, first_name, last_name, reg_no, email, password, gender, phone, role, college, is_added, is_banned, is_vitian, is_verified, is_profile_complete, is_leader, city, state, country) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19)",
user.ID,
user.FirstName,
user.LastName,
Expand All @@ -26,6 +26,7 @@ func InsertUser(user *models.User) error {
user.IsLeader,
user.City,
user.State,
user.Country,
)
return err
}
4 changes: 2 additions & 2 deletions internal/services/user/get_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ func FindUserByEmail(email string) (*models.User, error) {

var teamID uuid.NullUUID

err := database.DB.QueryRow("SELECT id, first_name, last_name, reg_no, password, phone, college, gender, role, is_banned, is_added, is_vitian, is_verified, is_profile_complete, is_leader, team_id, city, state FROM users WHERE email = $1",
err := database.DB.QueryRow("SELECT id, first_name, last_name, reg_no, password, phone, college, gender, role, is_banned, is_added, is_vitian, is_verified, is_profile_complete, is_leader, team_id, city, state, country FROM users WHERE email = $1",
email).
Scan(&user.ID, &user.FirstName, &user.LastName, &user.RegNo, &user.Password, &user.Phone,
&user.College, &user.Gender, &user.Role,
&user.IsBanned, &user.IsAdded, &user.IsVitian, &user.IsVerified, &user.IsProfileComplete, &user.IsLeader, &teamID, &user.City, &user.State)
&user.IsBanned, &user.IsAdded, &user.IsVitian, &user.IsVerified, &user.IsProfileComplete, &user.IsLeader, &teamID, &user.City, &user.State, &user.Country)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/services/user/update_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func UpdateUser(user *models.User) error {
_, err := database.DB.Exec(
`UPDATE users SET first_name = $1, last_name = $2,
reg_no = $3, phone = $4, gender = $5, college = $6, city = $7, state = $8, is_banned = $9, is_added = $10,
is_vitian = $11, is_verified = $12, is_profile_complete = $13 WHERE email = $14`,
is_vitian = $11, is_verified = $12, is_profile_complete = $13, country = $14 WHERE email = $15`,
user.FirstName,
user.LastName,
user.RegNo,
Expand All @@ -23,6 +23,7 @@ func UpdateUser(user *models.User) error {
user.IsVitian,
user.IsVerified,
user.IsProfileComplete,
user.Country,
user.Email,
)
return err
Expand Down

0 comments on commit 72dd24b

Please sign in to comment.