388 lines
9.1 KiB
Go
388 lines
9.1 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
func UpdateStudentName(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
if err := c.BodyParser(&data); err != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Failed to parse body",
|
|
"error": err,
|
|
})
|
|
}
|
|
|
|
// Ensure Authenticated admin sent request
|
|
if !AuthAdmin(c) {
|
|
cancel()
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Unauthorized: only an admin can perform this action",
|
|
})
|
|
}
|
|
|
|
// Check id and names are included
|
|
// Middle name is optional
|
|
if data["sid"] == "" || data["firstname"] == "" || data["lastname"] == "" {
|
|
cancel()
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "missing required fields",
|
|
})
|
|
}
|
|
|
|
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"firstname": data["firstname"],
|
|
"middlename": data["middlename"],
|
|
"lastname": data["lastname"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"sid": data["sid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateStudentGradeLevel(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
if err := c.BodyParser(&data); err != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Failed to parse body",
|
|
"error": err,
|
|
})
|
|
}
|
|
|
|
// Ensure Authorized admin sent request
|
|
if !AuthAdmin(c) {
|
|
cancel()
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Unauthorized: only an admin can perform this action",
|
|
})
|
|
}
|
|
|
|
// Check required fields are included
|
|
if data["sid"] == "" || data["gradelevel"] == "" {
|
|
cancel()
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "missing required fields",
|
|
})
|
|
}
|
|
|
|
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"gradelevel": data["gradelevel"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"sid": data["sid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateStudentHomeroom(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
if err := c.BodyParser(&data); err != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Failed to parse body",
|
|
"error": err,
|
|
})
|
|
}
|
|
|
|
// Ensure Authenticated admin sent request
|
|
if !AuthAdmin(c) {
|
|
cancel()
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Unauthorized: only an admin can perform this action",
|
|
})
|
|
}
|
|
|
|
// Check required fields are included
|
|
if data["sid"] == "" || data["homeroom"] == "" {
|
|
cancel()
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "missing required fields",
|
|
})
|
|
}
|
|
|
|
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"homeroom": data["homeroom"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := studentCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"sid": data["sid"]},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the student could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated student",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateStudentPassword(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateStudentLocker(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateStudentAddress(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateStudentYOG(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateStudentContacts(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateStudentPhoto(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateStudentEmail(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateTeacherHomeroom(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateTeacherPassword(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateTeacherAddress(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateTeacherPhoto(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateTeacherEmail(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateTeacherName(c *fiber.Ctx) error {
|
|
var data map[string]string
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
if err := c.BodyParser(&data); err != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Failed to parse body",
|
|
"error": err,
|
|
})
|
|
}
|
|
|
|
// Ensure Authenticated admin sent request
|
|
if !AuthAdmin(c) {
|
|
cancel()
|
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "Unauthorized: only an admin can perform this action",
|
|
})
|
|
}
|
|
|
|
// Check id and names are included
|
|
if data["_id"] == "" || data["firstname"] == "" || data["middlename"] == "" || data["lastname"] == "" {
|
|
cancel()
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "missing required fields",
|
|
})
|
|
}
|
|
|
|
teacherObjectId, idErr := primitive.ObjectIDFromHex(data["_id"])
|
|
if idErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "teacher not found",
|
|
"error": idErr,
|
|
})
|
|
}
|
|
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
"firstname": data["firstname"],
|
|
"middlename": data["middlename"],
|
|
"lastname": data["lastname"],
|
|
"updated_at": update_time,
|
|
},
|
|
}
|
|
|
|
result, updateErr := teacherCollection.UpdateOne(
|
|
ctx,
|
|
bson.M{"_id": teacherObjectId},
|
|
update,
|
|
)
|
|
if updateErr != nil {
|
|
cancel()
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"success": false,
|
|
"message": "the teacher could not be updated",
|
|
"error": updateErr,
|
|
})
|
|
}
|
|
defer cancel()
|
|
|
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
|
"success": true,
|
|
"message": "successfully updated teacher",
|
|
"result": result,
|
|
})
|
|
}
|
|
|
|
func UpdateContactName(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateContactAddress(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateContactPhone(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateContactEmail(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|
|
|
|
func UpdateContactPriority(c *fiber.Ctx) error {
|
|
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
|
"success": nil,
|
|
"message": "not implimented",
|
|
})
|
|
}
|