func (repo *DbOrderRepo) FindById(id int) domain.Order { row := repo.dbHandler.Query(fmt.Sprintf("SELECT customer_id FROM orders WHERE id = '%d' LIMIT 1", id)) var customerId int row.Next() row.Scan(&customerId) customerRepo := NewDbCustomerRepo(repo.dbHandlers) order := domain.Order{Id: id, Customer: customerRepo.FindById(customerId)} var itemId int itemRepo := NewDbItemRepo(repo.dbHandlers) row = repo.dbHandler.Query(fmt.Sprintf("SELECT item_id FROM items2orders WHERE order_id = '%d'", order.Id)) for row.Next() { row.Scan(&itemId) order.Add(itemRepo.FindById(itemId)) } return order }
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() } }