func SaveAll(ks []entity.Room, ps []entity.Expense, ss []entity.Deposit) error { var err error var session = database.GetDB().NewSession() defer session.Close() err = session.Begin() err = SaveRoom(ks, session) if err != nil { session.Rollback() return err } err = SaveExpense(ps, session) if err != nil { session.Rollback() return err } err = SaveDeposit(ss, session) if err != nil { session.Rollback() return err } err = session.Commit() return err }
func main() { database.InitDb("./student-hostel-admin.db") // Create ORM engine and database var err error var x = database.GetDB() // Sync tables if err = x.Sync(new(entity.User)); err != nil { log.Fatalf("Fail to sync database: %v\n", err) } if err = x.Sync(new(entity.Room)); err != nil { log.Fatalf("Fail to sync database: %v\n", err) } if err = x.Sync(new(entity.Expense)); err != nil { log.Fatalf("Fail to sync database: %v\n", err) } if err = x.Sync(new(entity.Deposit)); err != nil { log.Fatalf("Fail to sync database: %v\n", err) } for i := 0; i < 20; i++ { _, err = x.Insert(&entity.Expense{Year: 2015, Month: 9, Amount: 0, Notice: ""}) _, err = x.Insert(&entity.Deposit{Year: 2015, Month: 9, Amount: 0, Notice: ""}) } for i := 0; i < len(names); i++ { _, err = x.Insert(&entity.Room{Year: 2015, Month: 9, Roomnumber: int32(i + 1), Name: names[i], Rent: 0, Notice: ""}) } x.Insert(&entity.User{Username: "******", Password: encodeBase64("sugianto"), Role: "admin", Name: "Jony Sugianto"}) x.Insert(&entity.User{Username: "******", Password: encodeBase64("nugroho"), Role: "user", Name: "Widi Nugroho"}) x.Insert(&entity.User{Username: "******", Password: encodeBase64("wicaksono"), Role: "user", Name: "Kukuh Wicaksono"}) x.Insert(&entity.User{Username: "******", Password: encodeBase64("seto nugroho"), Role: "user", Name: "Pandu Seto Nugroho"}) }
func SaveUser(user *entity.User) error { var _, err = database.GetDB().InsertOne(user) database.Cache(user) return err }
func LoadDeposit(year int32, month int32) ([]entity.Deposit, error) { var ret []entity.Deposit var err = database.GetDB().Where("year = ? and month=?", year, month).Find(&ret) return ret, err }
func LoadExpense(year int32, month int32) ([]entity.Expense, error) { var ret []entity.Expense var err = database.GetDB().Where("year = ? and month=?", year, month).Find(&ret) return ret, err }
func LoadRoom(year int32, month int32) ([]entity.Room, error) { var ret []entity.Room var err = database.GetDB().Where("year = ? and month=?", year, month).Asc("roomnumber").Find(&ret) return ret, err }
func LoadUser(username string) (entity.User, error) { var user = entity.User{Username: username} _, err := database.GetDB().Get(&user) return user, err }