예제 #1
0
파일: user.go 프로젝트: atabek/gowebapp
// 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)
}
예제 #2
0
파일: note.go 프로젝트: atabek/gowebapp
// 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)
}
예제 #3
0
파일: note.go 프로젝트: atabek/gowebapp
// 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)
}
예제 #4
0
파일: note.go 프로젝트: atabek/gowebapp
// 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)
}
예제 #5
0
파일: student.go 프로젝트: atabek/gowebapp
// 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
파일: note.go 프로젝트: atabek/gowebapp
// NoteID returns the note id
func (u *Note) NoteID() string {
	r := ""

	switch database.ReadConfig().Type {
	case database.TypeMySQL:
		r = fmt.Sprintf("%v", u.ID)
	case database.TypeMongoDB:
		r = u.ObjectID.Hex()
	case database.TypeBolt:
		r = u.ObjectID.Hex()
	}

	return r
}
예제 #7
0
파일: user.go 프로젝트: atabek/gowebapp
// 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)
}
예제 #8
0
파일: student.go 프로젝트: atabek/gowebapp
// 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)
}
예제 #9
0
파일: student.go 프로젝트: atabek/gowebapp
// 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)
}
예제 #10
0
파일: student.go 프로젝트: atabek/gowebapp
// 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)
}
예제 #11
0
파일: note.go 프로젝트: atabek/gowebapp
// 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)
}
예제 #12
0
파일: student.go 프로젝트: atabek/gowebapp
// 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)
}