func DeleteBloodRequest(r *BloodRequest, ms *services.MySQL) (bool, *app.Msg, error) { status, error := r.ValidateDeleteReq() if status == false { return false, app.NewErrMsg(error), nil } query := "DELETE FROM requests WHERE id = ,?)" _, dbError := ms.Exec(query, r.ReqId) if dbError != nil { return false, nil, dbError } return false, app.NewErrMsg(error), nil }
func AuthenticateUser(mobile, password *string, ms *services.MySQL) (*User, *app.Msg) { u := User{} query := "SELECT id, password FROM users WHERE mobile = ?" dbError := ms.QueryRow(query, mobile).Scan(&u.Id, &u.Password) if dbError != nil { return nil, app.NewErrMsg("Invalid credentials") } else { err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(*password)) if err != nil { return nil, app.NewErrMsg("Invalid credentials") } return &u, nil } }
func CreateBloodRequest(r *BloodRequest, ms *services.MySQL) (bool, *app.Msg, error) { status, error := r.ValidateBloodReq() if status == false { return false, app.NewErrMsg(error), nil } query := "INSERT INTO requests(user_id, date_of_requirement, location, place_id, blood, comments, mobile) VALUES(?,?,POINT(" + r.Lat + ", " + r.Lng + "),?,?,?,?)" const createdFormat = "2006-01-02 15:04:05" //"Jan 2, 2006 at 3:04pm (MST)" utcTime, _ := strconv.ParseInt(r.Date, 10, 64) utcTime1 := time.Unix(utcTime, 0).Format(createdFormat) _, dbError := ms.Exec(query, r.UserId, utcTime1, r.PlaceId, r.Blood, r.Description, r.Phone) if dbError != nil { return false, nil, dbError } return false, app.NewErrMsg(error), nil }
func RegisterUser(u *User, ms *services.MySQL) (int64, *app.Msg, error) { status, error := ValidateUser(u, ms) if status == false { return 0, app.NewErrMsg(error), nil } passwordHash, _ := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) query := "INSERT INTO users(name, mobile, password, blood, sex, location, place_id) VALUES(?,?,?,?,?, POINT(" + u.Lat + ", " + u.Lng + "),?)" res, dbError := ms.Exec(query, u.Name, u.Mobile, string(passwordHash[:]), u.Blood, u.Sex, u.PlaceId) if dbError != nil { return 0, nil, dbError } else { id, err := res.LastInsertId() if err != nil { return 0, nil, err } else { return id, nil, nil } } return 0, app.NewErrMsg(error), nil }