update/student-model

This commit is contained in:
SowinskiBraeden committed 2022-01-15 11:34:25 -08:00
1 parent 20fb1529ed
commit 1b4d2d26db
3 files changed
+58 -37

No files matched your search

+24 -24
View File
@@ -80,31 +80,31 @@ func Enroll(c *fiber.Ctx) error {
} }
var student models.Student var student models.Student
student.FirstName = data["firstname"] student.PersonalData.FirstName = data["firstname"]
student.MiddleName = data["middlename"] student.PersonalData.MiddleName = data["middlename"]
student.LastName = data["lastname"] student.PersonalData.LastName = data["lastname"]
student.Age, _ = strconv.Atoi(data["age"]) student.PersonalData.Age, _ = strconv.Atoi(data["age"])
student.GradeLevel, _ = strconv.Atoi(data["gradelevel"]) student.SchoolData.GradeLevel, _ = strconv.Atoi(data["gradelevel"])
student.DOB = data["dob"] student.PersonalData.DOB = data["dob"]
student.Email = data["email"] student.PersonalData.Email = data["email"]
student.Province = data["province"] student.PersonalData.Province = data["province"]
student.City = data["city"] student.PersonalData.City = data["city"]
student.Address = data["address"] student.PersonalData.Address = data["address"]
student.Postal = data["postal"] student.PersonalData.Postal = data["postal"]
student.Contacts = []string{} student.PersonalData.Contacts = []string{}
student.YOG = ((12 - student.GradeLevel) + time.Now().Year()) + 1 student.SchoolData.YOG = ((12 - student.SchoolData.GradeLevel) + time.Now().Year()) + 1
student.SchoolEmail = student.GenerateSchoolEmail() student.SchoolData.SchoolEmail = student.GenerateSchoolEmail()
// Disable login block // Disable login block
student.AccountDisabled = false student.AccountData.AccountDisabled = false
student.Attempts = 0 student.AccountData.Attempts = 0
// Generate temporary password // Generate temporary password
tempPass := student.GeneratePassword(12, 1, 1, 1) tempPass := student.GeneratePassword(12, 1, 1, 1)
student.Password = student.HashPassword(tempPass) student.SchoolData.Password = student.HashPassword(tempPass)
student.TempPassword = true student.AccountData.TempPassword = true
// Send student personal email temp password // Send student personal email temp password
smtpHost := "smpt.gmail.com" smtpHost := "smpt.gmail.com"
@@ -114,7 +114,7 @@ func Enroll(c *fiber.Ctx) error {
auth := smtp.PlainAuth("", systemEmail, systemPassword, smtpHost) auth := smtp.PlainAuth("", systemEmail, systemPassword, smtpHost)
err := smtp.SendMail(smtpHost+":"+smtpPort, auth, systemEmail, []string{student.Email}, message) err := smtp.SendMail(smtpHost+":"+smtpPort, auth, systemEmail, []string{student.PersonalData.Email}, message)
if err != nil { if err != nil {
cancel() cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
@@ -131,7 +131,7 @@ func Enroll(c *fiber.Ctx) error {
break break
} }
} }
student.SID = sid student.SchoolData.SID = sid
student.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) student.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
student.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) student.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
@@ -370,7 +370,7 @@ func StudentLogin(c *fiber.Ctx) error {
} }
var localAccountDisabled = false var localAccountDisabled = false
if student.Attempts >= 5 { if student.AccountData.Attempts >= 5 {
localAccountDisabled = true // Catches newly disbaled account before student obj is updated localAccountDisabled = true // Catches newly disbaled account before student obj is updated
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
@@ -396,7 +396,7 @@ func StudentLogin(c *fiber.Ctx) error {
} }
} }
if localAccountDisabled || student.AccountDisabled { if localAccountDisabled || student.AccountData.AccountDisabled {
cancel() cancel()
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{ return c.Status(fiber.StatusForbidden).JSON(fiber.Map{
"success": false, "success": false,
@@ -409,7 +409,7 @@ func StudentLogin(c *fiber.Ctx) error {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{ update := bson.M{
"$set": bson.M{ "$set": bson.M{
"attempts": student.Attempts + 1, "attempts": student.AccountData.Attempts + 1,
"updated_at": update_time, "updated_at": update_time,
}, },
} }
@@ -435,7 +435,7 @@ func StudentLogin(c *fiber.Ctx) error {
defer cancel() defer cancel()
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{ claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
Issuer: student.SID, Issuer: student.SchoolData.SID,
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), // 1 Day ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), // 1 Day
}) })
token, err := claims.SignedString([]byte(SecretKey)) token, err := claims.SignedString([]byte(SecretKey))
+7
View File
@@ -291,6 +291,13 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
}) })
} }
func ResetStudentPassword(c *fiber.Ctx) error {
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
"succes": nil,
"message": "not implimented",
})
}
func UpdateStudentLocker(c *fiber.Ctx) error { func UpdateStudentLocker(c *fiber.Ctx) error {
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
"success": nil, "success": nil,
+27 -13
View File
@@ -11,24 +11,21 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
type Locker struct {
ID primitive.ObjectID `bson:"_id"`
LockerNumber string `json:"lockernumber"`
LockerPasscode string `json:"lockerpasscode"`
LockerType string `json:"lockertype"` // Upper / Lower locker
}
type Student struct { type Student struct {
ID primitive.ObjectID `bson:"_id"` ID primitive.ObjectID `bson:"_id"`
AccountDisabled bool `bson:"accountdisabled"` PersonalData struct {
FirstName string `json:"firstname" validate:"required"` FirstName string `json:"firstname" validate:"required"`
MiddleName string `json:"middlename"` MiddleName string `json:"middlename"`
LastName string `json:"lastname" validate:"required"` LastName string `json:"lastname" validate:"required"`
Age int `json:"age" validate:"required"` Age int `json:"age" validate:"required"`
GradeLevel int `json:"gradelevel" validate:"required"`
Email string `json:"email" validate:"required"` Email string `json:"email" validate:"required"`
SchoolEmail string `json:"schoolemail"`
Password string `json:"-" validate:"min=10,max=32"`
TempPassword bool `json:"temppassword"`
Attempts int `json:"attempts"` // login attempts max 5
SID string `json:"sid"` // Student ID
PEN string `json:"ped"` // Personal Education Number
Homeroom string `json:"homeroom"`
Locker string `json:"locker"`
YOG int `json:"yog"` // Year of Graduation
Address string `json:"address"` Address string `json:"address"`
City string `json:"city"` City string `json:"city"`
Province string `json:"province"` Province string `json:"province"`
@@ -36,6 +33,23 @@ type Student struct {
DOB string `json:"dob" validate:"required"` DOB string `json:"dob" validate:"required"`
Photo string `json:"photo"` Photo string `json:"photo"`
Contacts []string `json:"contacts"` // List of contact ID's rather than contact object Contacts []string `json:"contacts"` // List of contact ID's rather than contact object
}
SchoolData struct {
GradeLevel int `json:"gradelevel" validate:"required"`
SchoolEmail string `json:"schoolemail"`
Password string `json:"-" validate:"min=10,max=32"`
SID string `json:"sid"` // Student ID
PEN string `json:"ped"` // Personal Education Number
Homeroom string `json:"homeroom"`
Locker Locker `json:"locker"`
YOG int `json:"yog"` // Year of Graduation
Photo string `json:"photo"`
}
AccountData struct {
AccountDisabled bool `bson:"accountdisabled"`
TempPassword bool `json:"temppassword"`
Attempts int `json:"attempts"` // login attempts max 5
}
Created_at time.Time `json:"created_at"` Created_at time.Time `json:"created_at"`
Updated_at time.Time `json:"updated_at"` Updated_at time.Time `json:"updated_at"`
} }
@@ -46,13 +60,13 @@ func (s *Student) HashPassword(password string) string {
} }
func (s *Student) GenerateSchoolEmail() string { func (s *Student) GenerateSchoolEmail() string {
var email string = strings.ToLower(string(s.FirstName[0])) + "." + strings.ToLower(s.LastName) + "@surreyschools.ca" var email string = strings.ToLower(string(s.PersonalData.FirstName[0])) + "." + strings.ToLower(s.PersonalData.LastName) + "@surreyschools.ca"
// Add check to see if email already exists // Add check to see if email already exists
return email return email
} }
func (s *Student) ComparePasswords(password string) bool { func (s *Student) ComparePasswords(password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(s.Password), []byte(password)) err := bcrypt.CompareHashAndPassword([]byte(s.SchoolData.Password), []byte(password))
if err != nil { if err != nil {
return false return false
} }