Skip to content

Commit

Permalink
Merge pull request #11 from Killerrekt/master
Browse files Browse the repository at this point in the history
pr hopefull;y last for ideas projects teams
  • Loading branch information
shivamgutgutia authored Mar 4, 2024
2 parents 7bb8615 + 49ca01d commit dfcc4c0
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 69 deletions.
3 changes: 3 additions & 0 deletions db/migrations/003_ideas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ CREATE TABLE IF NOT EXISTS ideas (
title TEXT NOT NULL,
description TEXT NOT NULL,
track TEXT NOT NULL,
github TEXT NOT NULL default '',
figma TEXT NOT NULL default '',
others TEXT NOT NULL default '',
is_selected BOOLEAN NOT NULL DEFAULT false,
teamid UUID NOT NULL UNIQUE
);
Expand Down
57 changes: 4 additions & 53 deletions internal/controllers/project_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,7 @@ func CreateProject(ctx echo.Context) error {
}

func UpdateProject(ctx echo.Context) error {
var req struct {
Name *string `json:"name" validate:"min=1,max=50"`
Description *string `json:"description" validate:"min=50,max=200"`
Track *string `json:"track"`
GithubLink *string `json:"github_link" validate:"url"`
FigmaLink *string `json:"figma_link" validate:"url"`
Others *string `json:"others"`
}
var req models.CreateUpdateProjectRequest

if err := ctx.Bind(&req); err != nil {
return ctx.JSON(http.StatusBadRequest, response{
Expand All @@ -123,56 +116,14 @@ func UpdateProject(ctx echo.Context) error {
})
}

proj, err := services.GetProject(user.TeamID)
err := services.UpdateProject(req, user.TeamID)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, errors.New("invalid teamid")) {
return ctx.JSON(http.StatusExpectationFailed, response{
Message: "Failed to get project could be cause the user has not made an project",
Data: models.GetProject{},
Message: "The team has not created an project",
Status: false,
})
}
return ctx.JSON(http.StatusInternalServerError, response{
Message: "Failed to get project : " + err.Error(),
Status: false,
Data: models.GetProject{},
})
}

var data models.CreateUpdateProjectRequest

if req.Name == nil {
data.Name = proj.Name
} else {
data.Name = *req.Name
}

if req.Description == nil {
data.Description = proj.Description
} else {
data.Description = *req.Description
}

if req.GithubLink == nil {
data.GithubLink = proj.GithubLink
} else {
data.GithubLink = *req.GithubLink
}

if req.FigmaLink == nil {
data.FigmaLink = proj.FigmaLink
} else {
data.FigmaLink = *req.FigmaLink
}

if req.Others == nil {
data.Others = proj.Others
} else {
data.Others = *req.Others
}

err = services.UpdateProject(data, user.TeamID)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, response{
Message: "Failed to update the project" + err.Error(),
Status: false,
Expand Down
6 changes: 6 additions & 0 deletions internal/models/idea_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type CreateUpdateIdeasRequest struct {
Title string `json:"title" validate:"required,min=1,max=50"`
Description string `json:"description" validate:"required,min=50,max=200"`
Track string `json:"track" validate:"required"`
Github string `json:"github_link" validate:"url"`
Figma string `json:"figma_link" validate:"url"`
Others string `json:"others"`
}

type SelectIdeaRequest struct {
Expand All @@ -25,4 +28,7 @@ type GetIdea struct {
Title string `json:"title"`
Description string `json:"description"`
Track string `json:"track"`
Github string `json:"github_link"`
Figma string `json:"figma_link"`
Others string `json:"others"`
}
7 changes: 4 additions & 3 deletions internal/services/idea/create_idea.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ func CreateIdea(data models.CreateUpdateIdeasRequest, teamid uuid.UUID) error {
return err
}

query := `INSERT INTO ideas VALUES($1,$2,$3,$4,$5,$6) RETURNING id`
query := `INSERT INTO ideas VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING id`

var id uuid.UUID
err = database.DB.QueryRow(query, uuid.New(), data.Title, data.Description, data.Track, false, teamid).Scan(&id)
err = tx.QueryRow(query, uuid.New(), data.Title, data.Description,
data.Track, data.Github, data.Figma, data.Others, false, teamid).Scan(&id)

if err != nil {
tx.Rollback()
Expand All @@ -28,7 +29,7 @@ func CreateIdea(data models.CreateUpdateIdeasRequest, teamid uuid.UUID) error {

query = `UPDATE teams SET ideaid = $1 WHERE id = $2`

_, err = database.DB.Exec(query, id, teamid)
_, err = tx.Exec(query, id, teamid)
if err != nil {
tx.Rollback()
return err
Expand Down
5 changes: 3 additions & 2 deletions internal/services/idea/get_idea.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
)

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

var idea models.GetIdea

err := database.DB.QueryRow(query, teamid).Scan(&idea.Title, &idea.Description, &idea.Track)
err := database.DB.QueryRow(query, teamid).Scan(&idea.Title, &idea.Description,
&idea.Track, &idea.Github, &idea.Figma, &idea.Others)
return idea, err
}
4 changes: 2 additions & 2 deletions internal/services/idea/update_idea.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

func UpdateIdea(data models.CreateUpdateIdeasRequest, teamid uuid.UUID) error {
query := `UPDATE ideas SET title = $1, description = $2, track = $3 WHERE teamid = $4`
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(), &sql.TxOptions{Isolation: sql.LevelSerializable})
result, err := tx.Exec(query, data.Title, data.Description, data.Track, teamid)
result, err := tx.Exec(query, data.Title, data.Description, data.Track, data.Github, data.Figma, data.Others, teamid)
check, _ := result.RowsAffected()
if check == 0 {
tx.Rollback()
Expand Down
11 changes: 10 additions & 1 deletion internal/services/projects/update_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"context"
"database/sql"
"errors"

"github.com/CodeChefVIT/devsoc-backend-24/internal/database"
"github.com/CodeChefVIT/devsoc-backend-24/internal/models"
Expand All @@ -12,12 +13,20 @@ import (
func UpdateProject(data models.CreateUpdateProjectRequest, 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(), &sql.TxOptions{Isolation: sql.LevelSerializable})
_, err := tx.Exec(query, data.Name, data.Description, data.GithubLink, data.FigmaLink, data.Track, data.Others, teamid)
result, err := tx.Exec(query, data.Name, data.Description, data.GithubLink, data.FigmaLink, data.Track, data.Others, teamid)

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

check, _ := result.RowsAffected()

if check == 0 {
tx.Rollback()
return errors.New("invalid teamid")
}

err = tx.Commit()
if err != nil {
tx.Rollback()
Expand Down
13 changes: 7 additions & 6 deletions internal/services/team/get_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func FindTeamByTeamID(team_id uuid.UUID) (models.GetTeam, error) {

query := `SELECT teams.name,teams.code, teams.leader_id, teams.round ,
users.first_name, users.last_name, users.email, users.reg_no,
ideas.title, ideas.description, ideas.track ,
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 @@ -52,12 +52,13 @@ func FindTeamByTeamID(team_id uuid.UUID) (models.GetTeam, error) {
team.Round, _ = strconv.Atoi(values[3].String)
if values[8].Valid {
team.Ideas = models.GetIdea{Title: values[8].String,
Description: values[9].String, Track: values[10].String}
Description: values[9].String, Track: values[10].String,
Github: values[11].String, Figma: values[12].String, Others: values[13].String}
}
if values[11].Valid {
team.Project = models.GetProject{Name: values[11].String,
Description: values[12].String, GithubLink: values[13].String,
FigmaLink: values[14].String, Track: values[15].String, Others: values[16].String}
if values[14].Valid {
team.Project = models.GetProject{Name: values[14].String,
Description: values[15].String, GithubLink: values[16].String,
FigmaLink: values[17].String, Track: values[18].String, Others: values[19].String}
}
team.Users = append(team.Users, models.GetUser{FirstName: values[4].String,
LastName: values[5].String, Email: values[6].String,
Expand Down
3 changes: 1 addition & 2 deletions internal/services/user/create_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
)

func InsertUser(user models.User) error {

_, err := database.DB.Query(
_, 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, city, state) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)",
user.ID,
user.FirstName,
Expand Down

0 comments on commit dfcc4c0

Please sign in to comment.