Skip to content

Commit

Permalink
Add Courses Endpoints (#15)
Browse files Browse the repository at this point in the history
Signed-off-by: Md. Anisur Rahman <sunny.cse7575@gmail.com>
  • Loading branch information
anisurrahman75 authored Jul 13, 2024
1 parent 40ff37f commit 66f82ef
Show file tree
Hide file tree
Showing 23 changed files with 1,371 additions and 122 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
go.mongodb.org/mongo-driver v1.13.1
golang.org/x/crypto v0.14.0
google.golang.org/api v0.128.0
google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97
google.golang.org/grpc v1.60.1
)

Expand Down Expand Up @@ -56,6 +55,7 @@ require (
golang.org/x/time v0.3.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/protobuf v1.32.0 // indirect
Expand Down
152 changes: 152 additions & 0 deletions handlers/course/content.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
MIT License
Copyright (c) 2024 Praromvik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package course

import (
"encoding/json"
"fmt"
"net/http"
"reflect"

"github.com/praromvik/praromvik/models/course"
perror "github.com/praromvik/praromvik/pkg/error"

"github.com/go-chi/chi/v5"
)

type Content struct {
*course.Content
}

func (c *Content) Create(w http.ResponseWriter, r *http.Request) {
if err := json.NewDecoder(r.Body).Decode(&c.Content); err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on parsing JSON", err)
return
}
c.CourseRef = chi.URLParam(r, "courseRef")
errCode, err := course.ValidateNameUniqueness(c.Content)
if err != nil {
perror.HandleError(w, errCode, "", err)
return
}
if err := course.Create(c.Content); err != nil {
perror.HandleError(w, http.StatusBadRequest, "failed to create course content data into database", err)
return
}

if err := course.Sync(&course.Lesson{
CourseRef: c.Content.CourseRef,
}, "$push", c.LessonRef, "contents", c.ContentID); err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on syncing content to lesson", err)
}

w.WriteHeader(http.StatusOK)
}

func (c *Content) Get(w http.ResponseWriter, r *http.Request) {
c.Content = &course.Content{
CourseRef: chi.URLParam(r, "courseRef"),
ContentID: chi.URLParam(r, "id"),
}
// Fetch document from database
document, err := course.Get(c.Content)
if err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on getting course lesson", err)
return
}

// Check if the document is of the expected type
expectedType := reflect.TypeOf(&course.Content{})
if !isTypeValid(document, expectedType) {
perror.HandleError(w, http.StatusBadRequest, "", fmt.Errorf("document is not of type %s", expectedType))
return
}

// Encode the document to JSON and send the response
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(document.(*course.Content)); err != nil {
perror.HandleError(w, http.StatusInternalServerError, "Error on encoding JSON response", err)
return
}
w.WriteHeader(http.StatusOK)
}

func (c *Content) List(w http.ResponseWriter, r *http.Request) {
c.Content = &course.Content{
CourseRef: chi.URLParam(r, "courseRef"),
ContentID: chi.URLParam(r, "id"),
}
documents, err := course.List(c.Content)
if err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on getting course content list.", err)
}

// Check if the document is of the expected type
expectedType := reflect.TypeOf(&[]course.Content{})
if !isTypeValid(documents, expectedType) {
perror.HandleError(w, http.StatusBadRequest, "", fmt.Errorf("document is not of type %s", expectedType))
return
}

// Encode the documents to JSON and send the response
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(documents.(*[]course.Content)); err != nil {
perror.HandleError(w, http.StatusInternalServerError, "Error on encoding JSON response", err)
return
}
}

func (c *Content) Delete(w http.ResponseWriter, r *http.Request) {
c.Content = &course.Content{
CourseRef: chi.URLParam(r, "courseRef"),
ContentID: chi.URLParam(r, "id"),
}
// Fetch document from database
document, err := course.Get(c.Content)
if err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on getting course lesson", err)
return
}

// Check if the document is of the expected type
expectedType := reflect.TypeOf(&course.Content{})
if !isTypeValid(document, expectedType) {
perror.HandleError(w, http.StatusBadRequest, "", fmt.Errorf("document is not of type %s", expectedType))
return
}
c.Content.LessonRef = document.(*course.Content).LessonRef

if err := course.Delete(c.Content); err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on deleting content.", err)
}

if err := course.Sync(&course.Lesson{
CourseRef: c.Content.CourseRef,
}, "$pull", c.Content.LessonRef, "contents", c.ContentID); err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on syncing content to lesson", err)
}

w.WriteHeader(http.StatusOK)
}
115 changes: 104 additions & 11 deletions handlers/course/course.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
MIT License
# Copyright (c) 2024 Praromvik
Copyright (c) 2024 Praromvik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,32 +25,125 @@ SOFTWARE.
package course

import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"slices"

"github.com/praromvik/praromvik/models/course"
"github.com/praromvik/praromvik/pkg/auth"
perror "github.com/praromvik/praromvik/pkg/error"

"github.com/go-chi/chi/v5"
)

type Course struct {
*course.Course
}

func (o *Course) Create(w http.ResponseWriter, r *http.Request) {
fmt.Println("Create an Course")
func (c *Course) Create(w http.ResponseWriter, r *http.Request) {
if err := json.NewDecoder(r.Body).Decode(&c.Course); err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on parsing JSON", err)
return
}
errCode, err := course.ValidateNameUniqueness(c.Course)
if err != nil {
perror.HandleError(w, errCode, "", err)
return
}
info, err := auth.GetUserInfoFromSession(r)
if err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on getting session", err)
return
}
if !slices.Contains(c.Instructors, info.Name) {
c.Instructors = append(c.Instructors, info.Name)
}
if err := course.Create(c.Course); err != nil {
perror.HandleError(w, http.StatusBadRequest, "failed to course data into database", err)
return
}

w.WriteHeader(http.StatusOK)
}

func (c *Course) List(w http.ResponseWriter, r *http.Request) {
// Initialize Course instance
c.Course = &course.Course{}
// Fetch documents from the database
documents, err := course.List(c.Course)
if err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on getting course list.", err)
return
}

// Check if the document is of the expected type
expectedType := reflect.TypeOf(&[]course.Course{})
if !isTypeValid(documents, expectedType) {
perror.HandleError(w, http.StatusBadRequest, "", fmt.Errorf("document is not of type %s", expectedType))
return
}

// Encode the documents to JSON and send the response
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(documents.(*[]course.Course)); err != nil {
perror.HandleError(w, http.StatusInternalServerError, "Error on encoding JSON response", err)
return
}
}

func (o *Course) List(w http.ResponseWriter, r *http.Request) {
fmt.Println("List all Courses")
func (c *Course) Get(w http.ResponseWriter, r *http.Request) {
// Initialize Course instance
c.Course = &course.Course{}
c.CourseId = chi.URLParam(r, "id")
// Fetch document from database
document, err := course.Get(c.Course)
if err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on getting course.", err)
return
}

// Check if the document is of the expected type
expectedType := reflect.TypeOf(&course.Course{})
if !isTypeValid(document, expectedType) {
perror.HandleError(w, http.StatusBadRequest, "", fmt.Errorf("document is not of type %s", expectedType))
return
}

// Encode the document to JSON and send the response
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(document.(*course.Course)); err != nil {
perror.HandleError(w, http.StatusInternalServerError, "Error on encoding JSON response", err)
return
}
w.WriteHeader(http.StatusOK)
}

func (o *Course) GetByID(w http.ResponseWriter, r *http.Request) {
fmt.Println("Get an Course by ID")
func (c *Course) Update(w http.ResponseWriter, r *http.Request) {
c.Course = &course.Course{}
c.CourseId = chi.URLParam(r, "id")
if err := json.NewDecoder(r.Body).Decode(&c.Course); err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on parsing JSON", err)
return
}
if err := course.Update(c.Course); err != nil {
fmt.Println(err)
perror.HandleError(w, http.StatusBadRequest, "Error on updating course", err)
return
}
}

func (o *Course) UpdateByID(w http.ResponseWriter, r *http.Request) {
fmt.Println("Update an Course by ID")
func (c *Course) Delete(w http.ResponseWriter, r *http.Request) {
c.Course = &course.Course{}
c.CourseId = chi.URLParam(r, "id")
if err := course.Delete(c.Course); err != nil {
perror.HandleError(w, http.StatusBadRequest, "Error on deleting course.", err)
}
w.WriteHeader(http.StatusOK)
}

func (o *Course) DeleteByID(w http.ResponseWriter, r *http.Request) {
fmt.Println("Delete an Course by ID")
func isTypeValid(document interface{}, expectedType reflect.Type) bool {
docValue := reflect.ValueOf(document)
return docValue.Kind() == reflect.Ptr && docValue.Type() == expectedType
}
Loading

0 comments on commit 66f82ef

Please sign in to comment.