Skip to content

Commit

Permalink
feat: added organisers to a new file, added accept/reject endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikram-ghuku committed Nov 26, 2024
1 parent 2e1ed95 commit 7fc64f3
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 33 deletions.
88 changes: 88 additions & 0 deletions controllers/organiser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package controllers

import (
"fmt"
"net/http"

"github.com/kossiitkgp/kwoc-backend/v2/middleware"
"github.com/kossiitkgp/kwoc-backend/v2/models"
"github.com/kossiitkgp/kwoc-backend/v2/utils"
)

type AcceptRejectProject struct {
// Id of the project in the database (required)
Id uint `json:"id"`
// Status to be set of the project
ProjectStatus bool `json:"project_status"`
// Status Remark to be set of the project
StatusRemark string `json:"status_remark"`
}

func OrgFetchAllProjectDetails(w http.ResponseWriter, r *http.Request) {
app := r.Context().Value(middleware.APP_CTX_KEY).(*middleware.App)
db := app.Db
user_details := r.Context().Value(middleware.LOGIN_CTX_USERNAME_KEY).(utils.LoginJwtFields)

if user_details.UserType != "organiser" {
utils.LogErrAndRespond(r, w, nil, fmt.Sprintf("Error '%s' is not an organiser", user_details.Username), 400)
return
}

var projects []models.Project

tx := db.
Table("projects").
Preload("Mentor").
Preload("SecondaryMentor").
Select("id", "name", "description", "tags", "repo_link", "comm_channel", "readme_link", "mentor_id", "secondary_mentor_id", "project_status", "status_remark", "pull_count").
Find(&projects)

if tx.Error != nil {
utils.LogErrAndRespond(r, w, tx.Error, "Error fetching projects from the database.", http.StatusInternalServerError)
return
}

var response []Project = make([]Project, 0)

for _, project := range projects {
response = append(response, newProject(&project))
}

utils.RespondWithJson(r, w, response)
}

func UpdateStatusProject(w http.ResponseWriter, r *http.Request) {
app := r.Context().Value(middleware.APP_CTX_KEY).(*middleware.App)
db := app.Db
user_details := r.Context().Value(middleware.LOGIN_CTX_USERNAME_KEY).(utils.LoginJwtFields)

if user_details.UserType != OAUTH_TYPE_ORGANISER {
utils.LogErrAndRespond(r, w, nil, fmt.Sprintf("Error '%s' is not an organiser", user_details.Username), 400)
return
}

projectDetails := AcceptRejectProject{}

err := utils.DecodeJSONBody(r, &projectDetails)
if err != nil {
utils.LogErrAndRespond(r, w, err, "Error decoding request JSON body.", http.StatusBadRequest)
return
}

tx := db.
Table("projects").
Where("id = ?", projectDetails.Id).
Updates(projectDetails)

if tx.Error != nil {
utils.LogErrAndRespond(r, w, tx.Error, "Error updating the project.", http.StatusInternalServerError)
return
}

if projectDetails.ProjectStatus {
utils.RespondWithHTTPMessage(r, w, http.StatusOK, "Project accepted successfully.")
} else {
utils.RespondWithHTTPMessage(r, w, http.StatusOK, "Project Rejected successfully.")
}

}
33 changes: 0 additions & 33 deletions controllers/project_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,36 +165,3 @@ func FetchProjectDetails(w http.ResponseWriter, r *http.Request) {
response := newProject(&project)
utils.RespondWithJson(r, w, response)
}

func OrgFetchAllProjectDetails(w http.ResponseWriter, r *http.Request) {
app := r.Context().Value(middleware.APP_CTX_KEY).(*middleware.App)
db := app.Db
user_details := r.Context().Value(middleware.LOGIN_CTX_USERNAME_KEY).(utils.LoginJwtFields)

if user_details.UserType != "organiser" {
utils.LogErrAndRespond(r, w, nil, fmt.Sprintf("Error '%s' is not an organiser", user_details.Username), 400)
return
}

var projects []models.Project

tx := db.
Table("projects").
Preload("Mentor").
Preload("SecondaryMentor").
Select("id", "name", "description", "tags", "repo_link", "comm_channel", "readme_link", "mentor_id", "secondary_mentor_id", "project_status", "status_remark", "pull_count").
Find(&projects)

if tx.Error != nil {
utils.LogErrAndRespond(r, w, tx.Error, "Error fetching projects from the database.", http.StatusInternalServerError)
return
}

var response []Project = make([]Project, 0)

for _, project := range projects {
response = append(response, newProject(&project))
}

utils.RespondWithJson(r, w, response)
}
7 changes: 7 additions & 0 deletions server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,12 @@ func getRoutes(app *middleware.App) []Route {
middleware.WrapApp(app, controllers.FetchAllProjectStats),
false,
},
{
"Accept/Reject Project",
"POST",
"/project/updt_status",
middleware.WithLogin(middleware.WrapApp(app, controllers.UpdateStatusProject)),
false,
},
}
}

0 comments on commit 7fc64f3

Please sign in to comment.