Added list create and update methods
This commit is contained in:
@ -122,4 +122,24 @@ func IsErrIDCannotBeZero(err error) bool {
|
||||
|
||||
func (err ErrIDCannotBeZero) Error() string {
|
||||
return fmt.Sprintf("ID cannot be 0")
|
||||
}
|
||||
|
||||
// ===========
|
||||
// List errors
|
||||
// ===========
|
||||
|
||||
|
||||
// ErrListDoesNotExist represents a "ErrListDoesNotExist" kind of error. Used if the list does not exist.
|
||||
type ErrListDoesNotExist struct{
|
||||
ID int64
|
||||
}
|
||||
|
||||
// IsErrListDoesNotExist checks if an error is a ErrListDoesNotExist.
|
||||
func IsErrListDoesNotExist(err error) bool {
|
||||
_, ok := err.(ErrListDoesNotExist)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrListDoesNotExist) Error() string {
|
||||
return fmt.Sprintf("List does not exist [ID: %d]", err.ID)
|
||||
}
|
53
models/lists.go
Normal file
53
models/lists.go
Normal file
@ -0,0 +1,53 @@
|
||||
package models
|
||||
|
||||
type List struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
|
||||
Title string `xorm:"varchar(250)" json:"title"`
|
||||
Description string `xorm:"varchar(1000)" json:"description"`
|
||||
OwnerID int64 `xorm:"int(11)" json:"ownerID"`
|
||||
Owner User `xorm:"-" json:"owner"`
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
}
|
||||
|
||||
func GetListByID(id int64) (list List, err error){
|
||||
list.ID = id
|
||||
exists, err := x.Get(&list)
|
||||
if err != nil {
|
||||
return List{}, err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return List{}, ErrListDoesNotExist{ID: id}
|
||||
}
|
||||
|
||||
// Get the list owner
|
||||
user, _, err := GetUserByID(list.OwnerID)
|
||||
if err != nil {
|
||||
return List{}, err
|
||||
}
|
||||
|
||||
list.Owner = user
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func CreateOrUpdateList(list *List) (err error) {
|
||||
// Check if it exists
|
||||
_, err = GetListByID(list.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
list.OwnerID = list.Owner.ID
|
||||
|
||||
if list.ID == 0 {
|
||||
_, err = x.Insert(list)
|
||||
} else {
|
||||
_, err = x.ID(list.ID).Update(list)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
@ -8,7 +8,11 @@ import (
|
||||
_ "github.com/mattn/go-sqlite3" // Because.
|
||||
)
|
||||
|
||||
var x *xorm.Engine
|
||||
var (
|
||||
x *xorm.Engine
|
||||
|
||||
tables []interface{}
|
||||
)
|
||||
|
||||
func getEngine() (*xorm.Engine, error) {
|
||||
// Use Mysql if set
|
||||
@ -26,6 +30,13 @@ func getEngine() (*xorm.Engine, error) {
|
||||
return xorm.NewEngine("sqlite3", path)
|
||||
}
|
||||
|
||||
func init() {
|
||||
tables = append(tables,
|
||||
new(User),
|
||||
new(List),
|
||||
)
|
||||
}
|
||||
|
||||
// SetEngine sets the xorm.Engine
|
||||
func SetEngine() (err error) {
|
||||
x, err = getEngine()
|
||||
@ -40,7 +51,9 @@ func SetEngine() (err error) {
|
||||
x.SetMapper(core.GonicMapper{})
|
||||
|
||||
// Sync dat shit
|
||||
x.Sync(&User{})
|
||||
if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
|
||||
return fmt.Errorf("sync database struct error: %v", err)
|
||||
}
|
||||
|
||||
x.ShowSQL(Config.Database.ShowQueries)
|
||||
|
||||
|
@ -16,7 +16,7 @@ func CreateUser(user User) (newUser User, err error) {
|
||||
|
||||
// Check if the user already existst with that username
|
||||
existingUser, exists, err := GetUser(User{Username: newUser.Username})
|
||||
if err != nil && !IsErrUserDoesNotExist(err){
|
||||
if err != nil && !IsErrUserDoesNotExist(err) {
|
||||
return User{}, err
|
||||
}
|
||||
if exists {
|
||||
|
Reference in New Issue
Block a user