update/student-model
This commit is contained in:
1 parent
20fb1529ed
commit
1b4d2d26db
3 files changed
+73
-52
No files matched your search
@@ -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))
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
+42
-28
@@ -11,33 +11,47 @@ 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"`
|
Address string `json:"address"`
|
||||||
SchoolEmail string `json:"schoolemail"`
|
City string `json:"city"`
|
||||||
Password string `json:"-" validate:"min=10,max=32"`
|
Province string `json:"province"`
|
||||||
TempPassword bool `json:"temppassword"`
|
Postal string `json:"postal"`
|
||||||
Attempts int `json:"attempts"` // login attempts max 5
|
DOB string `json:"dob" validate:"required"`
|
||||||
SID string `json:"sid"` // Student ID
|
Photo string `json:"photo"`
|
||||||
PEN string `json:"ped"` // Personal Education Number
|
Contacts []string `json:"contacts"` // List of contact ID's rather than contact object
|
||||||
Homeroom string `json:"homeroom"`
|
}
|
||||||
Locker string `json:"locker"`
|
SchoolData struct {
|
||||||
YOG int `json:"yog"` // Year of Graduation
|
GradeLevel int `json:"gradelevel" validate:"required"`
|
||||||
Address string `json:"address"`
|
SchoolEmail string `json:"schoolemail"`
|
||||||
City string `json:"city"`
|
Password string `json:"-" validate:"min=10,max=32"`
|
||||||
Province string `json:"province"`
|
SID string `json:"sid"` // Student ID
|
||||||
Postal string `json:"postal"`
|
PEN string `json:"ped"` // Personal Education Number
|
||||||
DOB string `json:"dob" validate:"required"`
|
Homeroom string `json:"homeroom"`
|
||||||
Photo string `json:"photo"`
|
Locker Locker `json:"locker"`
|
||||||
Contacts []string `json:"contacts"` // List of contact ID's rather than contact object
|
YOG int `json:"yog"` // Year of Graduation
|
||||||
Created_at time.Time `json:"created_at"`
|
Photo string `json:"photo"`
|
||||||
Updated_at time.Time `json:"updated_at"`
|
}
|
||||||
|
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"`
|
||||||
|
Updated_at time.Time `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Student) HashPassword(password string) string {
|
func (s *Student) HashPassword(password string) string {
|
||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user