Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func MigrateDB() {
&schemas.Team{},
&schemas.Role{},
&schemas.Member{},
&schemas.Subject{},
&schemas.StudyMaterial{},
&schemas.SubjectCategory{},
} {
if err := db.AutoMigrate(&schema); err != nil {
panic(err)
Expand Down
14 changes: 10 additions & 4 deletions controllers/app_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

type AppController struct {
User interface{ UserController }
Team interface{ TeamController }
Seed interface{ SeedController }
User interface{ UserController }
Team interface{ TeamController }
Seed interface{ SeedController }
StudyMaterial interface{ StudyMaterialController }
}

type seedController struct {
Expand Down Expand Up @@ -41,6 +42,11 @@ func (s *seedController) SeedDB() error {
log.Panic(err)
return err
}
log.Println(color.GreenString("Seeded Successfully"))
err = s.seed.StudyMaterialSeeder()
if err != nil {
log.Panic(err)
return err
}
log.Println(color.GreenString(" Seeded Successfully"))
return nil
}
277 changes: 277 additions & 0 deletions controllers/resource_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
package controllers

import (
"log"
"net/http"

"github.com/ecea-nitt/ecea-server/middlewares"
"github.com/ecea-nitt/ecea-server/models"
"github.com/ecea-nitt/ecea-server/services"
"github.com/fatih/color"
"github.com/labstack/echo/v4"
)

type studyMaterialController struct {
rs services.StudyMaterialService
}

type StudyMaterialController interface {
AddStudyMaterial(c echo.Context) error

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a separate route for creating subject
params are subject_code, subject_name, subject_category

GetCategoryStudyMaterials(c echo.Context) error
GetAllMaterials(c echo.Context) error
GetStudyMaterial(c echo.Context) error

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a GetAll route

Expected Response:

"response": [
   { 
   "category": "Digital Circuits",
    "subjects":[
    {
      "name": "Basic VLSI Design",
      "subject": "VLSI",
      "subject_category": "DIGITAL ELECTRONICS",
      "SubjectCode": "ECPC13",
      "document_url": "http://localhost:8000/static/documents/2023-01-22T12:13:22Z.pdf"
     }
   ]
 }
]

AddSubject(c echo.Context) error
// UpdateStudyMaterialSubject(c echo.Context) error
UpdateStudyMaterialURL(c echo.Context) error
DeleteStudyMaterial(c echo.Context) error
}

func NewStudyMaterialController(rs services.StudyMaterialService) StudyMaterialController {
return &studyMaterialController{rs}
}

// AddStudyMaterial godoc
//
// @Summary Add a study material
// @Description Creates a new study material and adds to Database
// @Tags StudyMaterial
// @Accept multipart/form-data
// @Produce json
// @Param name formData string true "Enter material name"
// @Param code formData string true "Enter subject code"
// @Param document formData file true "Upload Document"
// @Success 200 {object} string
// @Failure 400 {object} models.Error
//
// @Security ApiKeyAuth
// @Router /v1/studymaterial/addMaterial [post]
func (rc *studyMaterialController) AddStudyMaterial(c echo.Context) error {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we will make new route to create a subject

remove subject_name just subject_code is neccessary

request := new(models.StudyMaterialRequest)
if err := c.Bind(request); err != nil {
log.Println(err)
return err
}
file, err := c.FormFile("document")
if err != nil {
log.Println(err)
return err
}
log.Println(request)
err = rc.rs.CreateNewStudyMaterial(*request, file)
if err != nil {
log.Println(err)
return middlewares.Responder(c, http.StatusConflict, "Conflict")
}
return middlewares.Responder(c, http.StatusOK, "Success")
}

// GetStudyMaterial godoc
//
// @Summary Get a study material
// @Description Fetches a study material and removes form database
// @Tags StudyMaterial
// @Accept json
// @Produce json
// @Param name path string true "Get study material"
// @Success 200 {object} models.StudyMaterials
// @Failure 400 {object} models.Error
//
// @Security ApiKeyAuth
// @Router /v1/studymaterial/get/{name} [get]
func (rc *studyMaterialController) GetStudyMaterial(c echo.Context) error {
rname := c.Param("name")
res, err := rc.rs.GetStudyMaterial(rname)
if err != nil {
log.Println(color.RedString(err.Error()))
return middlewares.Responder(c, http.StatusConflict, "Error Occurred")
}

return middlewares.Responder(c, http.StatusOK, &res)
}

// GetCategoryStudyMaterials godoc
//
// @Summary Get study materials from a category
// @Description Fetches all study materials belonging to a category from Database
// @Tags StudyMaterial
// @Accept json
// @Produce json
// @Param category path models.SubjectCategory true "Get study material from category"
// @Success 200 {object} []models.StudyMaterials
// @Failure 400 {object} models.Error
//
// @Security ApiKeyAuth
// @Router /v1/studymaterial/getcat/{category} [get]
func (rc *studyMaterialController) GetCategoryStudyMaterials(c echo.Context) error {
/*
request := new(models.StudyMaterialRequest)
if err := c.Bind(request); err != nil {
return err
}
*/
res, err := rc.rs.GetCategoryStudyMaterials(c.Param("category"))
log.Println(c.Param("category"))
if err != nil {
log.Println(color.RedString(err.Error()))
return middlewares.Responder(c, http.StatusConflict, "Conflict")
}
return middlewares.Responder(c, http.StatusOK, res)
}

// UpdateStudyMaterialSubject godoc
//
// @Summary Updates a studymaterial's subject name
// @Description Updates a subject name and updates to Database
// @Tags StudyMaterial
// @Accept multipart/form-data
// @Produce json
// @Param subject formData string true "Edit name"
// @Param subjectCode formData string true "Enter subject code"
// @Success 200 {object} string
// @Failure 400 {object} models.Error
//
// @Security ApiKeyAuth
// @Router /v1/studymaterial/edit/subject [put]

/*
func (rc *studyMaterialController) UpdateStudyMaterialSubject(c echo.Context) error {

request := new(models.StudyMaterialRequest)
if err := c.Bind(request); err != nil {
log.Println(err)
return middlewares.Responder(c, http.StatusBadRequest, "Bad Request")
}
subject := c.Param("subject")
subjectCode := c.Param("subjectCode")
if err := c.Bind(request); err != nil {
log.Println(err)
return middlewares.Responder(c, http.StatusBadRequest, "Bad Request")
}
log.Println(request.Subject, request.SubjectCode)
err := rc.rs.EditStudyMaterialSubject(request.Subject, request.SubjectCode)
if err != nil {
log.Println(color.RedString(err.Error()))
return middlewares.Responder(c, http.StatusConflict, err)
}
return middlewares.Responder(c, http.StatusOK, "Success")
}
*/

// UpdateStudyMaterialURL godoc
//
// @Summary Update a study material's document
// @Description Update a document and update it on Database
// @Tags StudyMaterial
// @Accept multipart/form-data
// @Produce json
// @Param name formData string true "Enter the name of material"
// @Param document formData file true "Edit Document"
// @Success 200 {object} string
// @Failure 400 {object} models.Error
//
// @Security ApiKeyAuth
// @Router /v1/studymaterial/edit/document [put]
func (rc *studyMaterialController) UpdateStudyMaterialURL(c echo.Context) error {

request := new(models.StudyMaterialRequest)
if err := c.Bind(request); err != nil {
log.Println(err)
return middlewares.Responder(c, http.StatusBadRequest, "Bad Request")
}
/*
name := c.Param("name")
request := new(models.StudyMaterialRequest)
if err := c.Bind(request); err != nil {
log.Println(err)
return middlewares.Responder(c, http.StatusBadRequest, "Bad Request")
}
*/
file, err := c.FormFile("document")
if err != nil {
log.Println(err)
return err
}

err = rc.rs.EditStudyMaterialURL(request.Name, file)
if err != nil {
log.Println(color.RedString(err.Error()))
return middlewares.Responder(c, http.StatusConflict, err)
}
return middlewares.Responder(c, http.StatusOK, "Success")
}

// DeleteStudyMaterial godoc
//
// @Summary Deletes a study material
// @Description Deletes a study material along with its document and remove form Database
// @Tags StudyMaterial
// @Accept json
// @Produce json
// @Param name path string true "Delete study material"
// @Success 200 {object} string
// @Failure 400 {object} models.Error
//
// @Security ApiKeyAuth
// @Router /v1/studymaterial/delete/{name} [delete]
func (rc *studyMaterialController) DeleteStudyMaterial(c echo.Context) error {
rname := c.Param("name")

err := rc.rs.RemoveStudyMaterial(rname)
log.Println(rname)
if err != nil {
log.Println(color.RedString(err.Error()))
return middlewares.Responder(c, http.StatusConflict, "Error Occurred")
}

return middlewares.Responder(c, http.StatusOK, "Success")
}

// AddSubject godoc
//
// @Summary Add a subject
// @Description Creates a new subject and adds to Database
// @Tags StudyMaterial
// @Accept multipart/form-data
// @Produce json
// @Param subject formData string true "Enter subject name"
// @Param code formData string true "Enter subject code"
// @Param category formData models.SubjectCategory true "Enter subject category"
// @Success 200 {object} string
// @Failure 400 {object} models.Error
//
// @Security ApiKeyAuth
// @Router /v1/studymaterial/addSubject [post]
func (rc *studyMaterialController) AddSubject(c echo.Context) error {
response := new(models.StudyMaterialRequest)
if err := c.Bind(response); err != nil {
return err
}
err := rc.rs.AddSubject(response)
if err != nil {
log.Println(color.RedString(err.Error()))
return middlewares.Responder(c, http.StatusConflict, "Error Occurred")
}

return middlewares.Responder(c, http.StatusOK, "Success")
}

// GetAllStudyMaterials godoc
//
// @Summary Get all study materials from each category
// @Description Fetches all study materials belonging to every category from Database
// @Tags StudyMaterial
// @Accept json
// @Produce json
// @Success 200 {object} []models.CategoryMaterials
// @Failure 400 {object} models.Error
//
// @Security ApiKeyAuth
// @Router /v1/studymaterial/getall [get]
func (rc *studyMaterialController) GetAllMaterials(c echo.Context) error {
res, err := rc.rs.GetAllMaterials()
if err != nil {
log.Println(color.RedString(err.Error()))
return middlewares.Responder(c, http.StatusConflict, "Conflict")
}
return middlewares.Responder(c, http.StatusOK, res)
}
2 changes: 2 additions & 0 deletions controllers/team_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func (tc *teamController) AddMember(c echo.Context) error {
log.Println(err)
return middlewares.Responder(c, http.StatusBadRequest, "Bad Request")
}
log.Println(request.Team)
log.Println(request.Role)
file, err := c.FormFile("image")
if err != nil {
log.Println(err)
Expand Down
14 changes: 8 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ services:
ports:
- ${POSTGRES_PORT}:${POSTGRES_PORT}
command: -p ${POSTGRES_PORT}
volumes:
- ./database:/data/postgres
# volumes:
# - database:/data/postgres
#volumes:
# database:

networks:
default:
external:
name: thirumathikart_network
# networks:
# default:
# external:
# name: thirumathikart_network
Loading