コード例 #1
0
func (repo *DbItemRepo) FindById(id int) domain.Item {
	row := repo.dbHandler.Query(fmt.Sprintf("SELECT name, value, available FROM items WHERE id = '%d' LIMIT 1", id))
	var name string
	var value float64
	var available string
	row.Next()
	row.Scan(&name, &value, &available)
	item := domain.Item{Id: id, Name: name, Value: value}
	item.Available = false
	if available == "yes" {
		item.Available = true
	}
	return item
}
コード例 #2
0
func Test_OrderRepository(t *testing.T) {
	dbHandler := infrastructure.NewSqliteHandler("/var/tmp/test2.sqlite")
	dbHandler.Execute("DROP TABLE users")
	dbHandler.Execute("DROP TABLE customers")
	dbHandler.Execute("DROP TABLE orders")
	dbHandler.Execute("DROP TABLE items")
	dbHandler.Execute("DROP TABLE items2orders")
	dbHandler.Execute("CREATE TABLE users (id INTEGER, customer_id INTEGER, is_admin VARCHAR(3))")
	dbHandler.Execute("CREATE TABLE customers (id INTEGER, name VARCHAR(42))")
	dbHandler.Execute("CREATE TABLE orders (id INTEGER, customer_id INTEGER)")
	dbHandler.Execute("CREATE TABLE items (id INTEGER, name VARCHAR(42), value FLOAT, available VARCHAR(3))")
	dbHandler.Execute("CREATE TABLE items2orders (item_id INTEGER, order_id INTEGER)")

	handlers := make(map[string]interfaces.DbHandler)
	handlers["DbUserRepo"] = dbHandler
	handlers["DbCustomerRepo"] = dbHandler
	handlers["DbOrderRepo"] = dbHandler
	handlers["DbItemRepo"] = dbHandler

	ur := interfaces.NewDbUserRepo(handlers)
	or := interfaces.NewDbOrderRepo(handlers)
	ir := interfaces.NewDbItemRepo(handlers)

	c := domain.Customer{}
	c.Id = 555
	c.Name = "John Doe"
	u := usecases.User{}
	u.Id = 6
	u.IsAdmin = true
	u.Customer = c
	ur.Store(u)

	i1 := domain.Item{}
	i1.Id = 101
	i1.Name = "Car"
	i1.Value = 125.45
	i1.Available = true
	ir.Store(i1)

	i2 := domain.Item{}
	i2.Id = 102
	i2.Name = "Table"
	i2.Value = 432.56
	i2.Available = false
	ir.Store(i2)

	i3 := domain.Item{}
	i3.Id = 103
	i3.Name = "Chair"
	i3.Value = 52.31
	i3.Available = true
	ir.Store(i3)

	o := domain.Order{}
	o.Id = 39
	o.Customer = c
	o.Add(i1)
	o.Add(i2) //fails because it's not available
	o.Add(i3)
	or.Store(o)

	o = or.FindById(39)

	if o.Customer.Name != "John Doe" {
		t.Error()
	}

	items := o.Items
	if items[1].Id != 103 {
		t.Error()
	}
}