Logout and contact stuff

This commit is contained in:
SowinskiBraeden committed 2021-10-10 16:31:02 -07:00
1 parent 7d649ef05b
commit b20e1f04c5
3 files changed
+138 -5

No files matched your search

+125 -4
View File
@@ -56,6 +56,7 @@ func Enroll(c *fiber.Ctx) error {
student.City = data["city"]
student.Address = data["address"]
student.Postal = data["postal"]
student.Contacts = []string{}
student.YOG = ((12 - student.GradeLevel) + time.Now().Year()) + 1
@@ -345,6 +346,22 @@ func Teacher(c *fiber.Ctx) error {
})
}
// Should work for both teacher and student ends
func Logout(c *fiber.Ctx) error {
cookie := fiber.Cookie{
Name: "jwt",
Value: "",
Expires: time.Now().Add(-time.Hour),
HTTPOnly: true,
}
c.Cookie(&cookie)
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true,
"message": "successfully logged out",
})
}
func UpdateStudentName(c *fiber.Ctx) error {
var data map[string]string
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
@@ -637,7 +654,7 @@ func CreateContact(c *fiber.Ctx) error {
}
// Check required fields are included
if data["firstname"] == "" || data["lastname"] == "" || data["homephone"] == "" || data["email"] == "" || data["priority"] == "" || data["relation"] == "" {
if data["sid"] == "" || data["firstname"] == "" || data["lastname"] == "" || data["homephone"] == "" || data["email"] == "" || data["priority"] == "" || data["relation"] == "" {
cancel()
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false,
@@ -668,19 +685,123 @@ func CreateContact(c *fiber.Ctx) error {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "the contact could not be inserted",
"message": "could not insert contact",
"error": insertErr,
})
}
update := bson.M{
"$push": bson.M{
"contacts": contact.ID,
},
}
_, 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 inserted contact",
"message": "successfully inserted contact to student",
})
}
func UpdateContact(c *fiber.Ctx) error {
func RemoveContact(c *fiber.Ctx) error {
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
"success": nil,
"message": "not implimented",
})
}
func UpdateContactName(c *fiber.Ctx) error {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Failed to parse body",
"error": err,
})
}
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
"success": nil,
"message": "not implimented",
})
}
func UpdateContactAddress(c *fiber.Ctx) error {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Failed to parse body",
"error": err,
})
}
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
"success": nil,
"message": "not implimented",
})
}
func UpdateContactPhone(c *fiber.Ctx) error {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Failed to parse body",
"error": err,
})
}
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
"success": nil,
"message": "not implimented",
})
}
func UpdateContactEmail(c *fiber.Ctx) error {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Failed to parse body",
"error": err,
})
}
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
"success": nil,
"message": "not implimented",
})
}
func UpdateContactPriority(c *fiber.Ctx) error {
var data map[string]string
if err := c.BodyParser(&data); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Failed to parse body",
"error": err,
})
}
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
"success": nil,
"message": "not implimented",
+1 -1
View File
@@ -33,7 +33,7 @@ type Student struct {
Postal string `json:"pc"` // Postal Code
DOB string `json:"dob" validate:"required"`
Photo string `json:"photo"`
Contacts []Contact `json:"contacts"`
Contacts []string `json:"contacts"` // List of contact ID's rather than contact object
Created_at time.Time `json:"created_at"`
Updated_at time.Time `json:"updated_at"`
}
+12
View File
@@ -31,6 +31,15 @@ func Setup(app *fiber.App) {
app.Post("/api/student/updatePhoto", controllers.UpdateStudentPhoto)
app.Post("/api/student/updateEmail", controllers.UpdateStudentEmail)
// Student Contacts
app.Post("/api/student/createContact", controllers.CreateContact)
app.Post("/api/student/contact/updateName", controllers.UpdateContactName)
app.Post("/api/student/contact/updateAddress", controllers.UpdateContactAddress)
app.Post("/api/student/contact/updatePhone", controllers.UpdateContactPhone)
app.Post("/api/student/contact/updateEmail", controllers.UpdateContactEmail)
app.Post("/api/student/contact/updatePriority", controllers.UpdateContactPriority)
app.Post("/api/student/contact/removeContact", controllers.RemoveContact)
// Login + Register handling
app.Get("/api/teacher", controllers.Teacher)
app.Post("/api/teacher/register", controllers.RegisterTeacher)
@@ -43,4 +52,7 @@ func Setup(app *fiber.App) {
app.Post("/api/teacher/updateName", controllers.UpdateTeacherName)
app.Post("/api/teacher/updateHomeroom", controllers.UpdateTeacherHomeroom)
app.Post("/api/teacher/updateEmail", controllers.UpdateTeacherEmail)
// General Routes
app.Post("/api/logout", controllers.Logout)
}