Пример #1
0
// NoteDelete deletes a note
func NoteDelete(userID string, noteID string) error {
	var err error

	if database.CheckConnection() {
		// Create a copy of mongo
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("note")

		var note Note
		note, err = NoteByID(userID, noteID)
		if err == nil {
			// Confirm the owner is attempting to modify the note
			if note.UserID.Hex() == userID {
				err = c.RemoveId(bson.ObjectIdHex(noteID))
			} else {
				err = ErrUnauthorized
			}
		}
	} else {
		err = ErrUnavailable
	}

	return standardizeError(err)
}
Пример #2
0
// UserCreate creates user
func UserCreate(first_name, last_name, email, password string) error {
	var err error

	now := time.Now()

	if database.CheckConnection() {
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("user")

		user := &User{
			ObjectId:   bson.NewObjectId(),
			First_name: first_name,
			Last_name:  last_name,
			Email:      email,
			Password:   password,
			Status_id:  1,
			Created_at: now,
			Updated_at: now,
			Deleted:    0,
		}
		err = c.Insert(user)
	} else {
		err = ErrUnavailable
	}

	return standardizeError(err)
}
Пример #3
0
// NoteCreate creates a note
func NoteCreate(content string, userID string) error {
	var err error

	now := time.Now()

	if database.CheckConnection() {
		// Create a copy of mongo
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("note")

		note := &Note{
			ObjectID:  bson.NewObjectId(),
			Content:   content,
			UserID:    bson.ObjectIdHex(userID),
			CreatedAt: now,
			UpdatedAt: now,
			Deleted:   0,
		}
		err = c.Insert(note)
	} else {
		err = ErrUnavailable
	}

	return standardizeError(err)
}
Пример #4
0
// NoteByID gets note by ID
func NoteByID(userID string, noteID string) (Note, error) {
	var err error

	result := Note{}

	if database.CheckConnection() {
		// Create a copy of mongo
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("note")

		// Validate the object id
		if bson.IsObjectIdHex(noteID) {
			err = c.FindId(bson.ObjectIdHex(noteID)).One(&result)
			if result.UserID != bson.ObjectIdHex(userID) {
				result = Note{}
				err = ErrUnauthorized
			}
		} else {
			err = ErrNoResult
		}
	} else {
		err = ErrUnavailable
	}

	return result, standardizeError(err)
}
Пример #5
0
// StudentUpdate updates a student
func StudentUpdate(studentID, firstName, lastName, grade string) error {
	var err error

	now := time.Now()

	if database.CheckConnection() {
		// Create a copy of mongo
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("student")
		var student Student
		student, err = StudentBySID(studentID)
		if err == nil {
			student.Updated_at = now
			student.First_name = firstName
			student.Last_name = lastName
			student.Grade = grade
			err = c.UpdateId(bson.ObjectIdHex(student.ID()), &student)
		} else {
			err = ErrUnauthorized
		}
	} else {
		err = ErrUnavailable
	}

	return standardizeError(err)
}
Пример #6
0
// UserByEmail gets user information from email
func UserByEmail(email string) (User, error) {
	var err error

	result := User{}

	if database.CheckConnection() {
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("user")
		err = c.Find(bson.M{"email": email}).One(&result)
	} else {
		err = ErrUnavailable
	}

	return result, standardizeError(err)
}
Пример #7
0
// StudentBySID gets student information from student school id
func StudentBySID(sid string) (Student, error) {
	var err error

	result := Student{}

	if database.CheckConnection() {
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("student")
		err = c.Find(bson.M{"student_id": sid}).One(&result)
	} else {
		err = ErrUnavailable
	}

	return result, standardizeError(err)
}
Пример #8
0
// StudentsGet gets students
func StudentsGet() ([]Student, error) {
	var err error

	// List all students
	var students = make([]Student, 0)

	if database.CheckConnection() {
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("student")
		err = c.Find(nil).All(&students)
	} else {
		err = ErrUnavailable
	}

	return students, standardizeError(err)
}
Пример #9
0
// StudentDelete deletes a student
func StudentDelete(studentID string) error {
	var err error

	if database.CheckConnection() {
		// Create a copy of mongo
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("student")

		var student Student
		student, err = StudentBySID(studentID)
		fmt.Println(student)
		if err == nil {
			err = c.RemoveId(bson.ObjectIdHex(student.ID()))
		}
	} else {
		err = ErrUnavailable
	}

	return standardizeError(err)
}
Пример #10
0
// NotesByUserID gets all notes for a user
func NotesByUserID(userID string) ([]Note, error) {
	var err error

	var result []Note

	if database.CheckConnection() {
		// Create a copy of mongo
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("note")

		// Validate the object id
		if bson.IsObjectIdHex(userID) {
			err = c.Find(bson.M{"user_id": bson.ObjectIdHex(userID)}).All(&result)
		} else {
			err = ErrNoResult
		}
	} else {
		err = ErrUnavailable
	}

	return result, standardizeError(err)
}
Пример #11
0
// StudentCreate creates student
func StudentCreate(first_name, last_name, grade, escaped_student_id string,
	balance float64, caretype int64, fivedays, freereduced bool) error {

	fmt.Println("fivedays: ", fivedays)
	fmt.Println("caretype: ", caretype)
	fmt.Println("freereduced: ", freereduced)
	fmt.Println("balanace: ", balance)

	var err error
	now := time.Now()

	if database.CheckConnection() {
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("student")

		student := &Student{
			ObjectId:    bson.NewObjectId(),
			First_name:  first_name,
			Last_name:   last_name,
			Grade:       grade,
			Student_id:  escaped_student_id,
			Created_at:  now,
			Updated_at:  now,
			Deleted:     0,
			FiveDays:    fivedays,
			CareType:    caretype,
			FreeReduced: freereduced,
			Balance:     balance,
		}
		err = c.Insert(student)
	} else {
		err = ErrUnavailable
	}

	return standardizeError(err)
}