-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e452e33
commit c44af62
Showing
10 changed files
with
537 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package model | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/josephspurrier/gowebapp/shared/database" | ||
|
||
"gopkg.in/mgo.v2/bson" | ||
) | ||
|
||
// ***************************************************************************** | ||
// User | ||
// ***************************************************************************** | ||
|
||
// User table contains the information for each user | ||
type User struct { | ||
ObjectId bson.ObjectId `bson:"_id"` | ||
First_name string `db:"first_name" bson:"first_name"` | ||
Last_name string `db:"last_name" bson:"last_name"` | ||
Email string `db:"email" bson:"email"` | ||
Password string `db:"password" bson:"password"` | ||
Status_id uint8 `db:"status_id" bson:"status_id"` | ||
Created_at time.Time `db:"created_at" bson:"created_at"` | ||
Updated_at time.Time `db:"updated_at" bson:"updated_at"` | ||
Deleted uint8 `db:"deleted" bson:"deleted"` | ||
} | ||
|
||
var ( | ||
ErrCode = errors.New("Case statement in code is not correct.") | ||
ErrNoResult = errors.New("Result not found.") | ||
ErrUnavailable = errors.New("Database is unavailable.") | ||
) | ||
|
||
// Id returns the user id | ||
func (u *User) ID() string { | ||
return u.ObjectId.Hex() | ||
} | ||
|
||
// standardizeErrors returns the same error regardless of the database used | ||
func standardizeError(err error) error { | ||
return err | ||
} | ||
|
||
// UserByEmail gets user information from email | ||
func UserByEmail(email string) (User, error) { | ||
var err error | ||
|
||
result := User{} | ||
|
||
err = database.View("user", email, &result) | ||
if err != nil { | ||
err = ErrNoResult | ||
} | ||
|
||
return result, standardizeError(err) | ||
} | ||
|
||
// UserCreate creates user | ||
func UserCreate(first_name, last_name, email, password string) error { | ||
var err error | ||
|
||
now := time.Now() | ||
|
||
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 = database.Update("user", user.Email, &user) | ||
|
||
return standardizeError(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package model | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/josephspurrier/gowebapp/shared/database" | ||
|
||
"gopkg.in/mgo.v2" | ||
"gopkg.in/mgo.v2/bson" | ||
) | ||
|
||
// ***************************************************************************** | ||
// User | ||
// ***************************************************************************** | ||
|
||
// User table contains the information for each user | ||
type User struct { | ||
ObjectId bson.ObjectId `bson:"_id"` | ||
First_name string `db:"first_name" bson:"first_name"` | ||
Last_name string `db:"last_name" bson:"last_name"` | ||
Email string `db:"email" bson:"email"` | ||
Password string `db:"password" bson:"password"` | ||
Status_id uint8 `db:"status_id" bson:"status_id"` | ||
Created_at time.Time `db:"created_at" bson:"created_at"` | ||
Updated_at time.Time `db:"updated_at" bson:"updated_at"` | ||
Deleted uint8 `db:"deleted" bson:"deleted"` | ||
} | ||
|
||
var ( | ||
ErrCode = errors.New("Case statement in code is not correct.") | ||
ErrNoResult = errors.New("Result not found.") | ||
ErrUnavailable = errors.New("Database is unavailable.") | ||
) | ||
|
||
// Id returns the user id | ||
func (u *User) ID() string { | ||
return u.ObjectId.Hex() | ||
} | ||
|
||
// standardizeErrors returns the same error regardless of the database used | ||
func standardizeError(err error) error { | ||
if err == mgo.ErrNotFound { | ||
return ErrNoResult | ||
} | ||
|
||
return err | ||
} | ||
|
||
// 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) | ||
} | ||
|
||
// 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package model | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/josephspurrier/gowebapp/shared/database" | ||
) | ||
|
||
// ***************************************************************************** | ||
// User | ||
// ***************************************************************************** | ||
|
||
// User table contains the information for each user | ||
type User struct { | ||
Id uint32 `db:"id"` | ||
First_name string `db:"first_name"` | ||
Last_name string `db:"last_name"` | ||
Email string `db:"email"` | ||
Password string `db:"password"` | ||
Status_id uint8 `db:"status_id"` | ||
Created_at time.Time `db:"created_at"` | ||
Updated_at time.Time `db:"updated_at"` | ||
Deleted uint8 `db:"deleted"` | ||
} | ||
|
||
// User_status table contains every possible user status (active/inactive) | ||
type User_status struct { | ||
Id uint8 `db:"id"` | ||
Status string `db:"status"` | ||
Created_at time.Time `db:"created_at"` | ||
Updated_at time.Time `db:"updated_at"` | ||
Deleted uint8 `db:"deleted"` | ||
} | ||
|
||
var ( | ||
ErrCode = errors.New("Case statement in code is not correct.") | ||
ErrNoResult = errors.New("Result not found.") | ||
ErrUnavailable = errors.New("Database is unavailable.") | ||
) | ||
|
||
// Id returns the user id | ||
func (u *User) ID() string { | ||
return fmt.Sprintf("%v", u.Id) | ||
} | ||
|
||
// standardizeErrors returns the same error regardless of the database used | ||
func standardizeError(err error) error { | ||
if err == sql.ErrNoRows { | ||
return ErrNoResult | ||
} | ||
|
||
return err | ||
} | ||
|
||
// UserByEmail gets user information from email | ||
func UserByEmail(email string) (User, error) { | ||
result := User{} | ||
err := database.Sql.Get(&result, "SELECT id, password, status_id, first_name FROM user WHERE email = ? LIMIT 1", email) | ||
return result, err | ||
} | ||
|
||
// UserIdByEmail gets user id from email | ||
func UserIdByEmail(email string) (User, error) { | ||
result := User{} | ||
err := database.Sql.Get(&result, "SELECT id FROM user WHERE email = ? LIMIT 1", email) | ||
return result, err | ||
} | ||
|
||
// UserCreate creates user | ||
func UserCreate(first_name, last_name, email, password string) error { | ||
_, err := database.Sql.Exec("INSERT INTO user (first_name, last_name, email, password) VALUES (?,?,?,?)", first_name, last_name, email, password) | ||
return err | ||
} |
Oops, something went wrong.