|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Data; |
| 4 | +using System.Linq; |
| 5 | +using NUnit.Framework; |
| 6 | +using ServiceStack.DataAnnotations; |
| 7 | +using ServiceStack.Logging; |
| 8 | + |
| 9 | +namespace ServiceStack.OrmLite.Tests.UseCase |
| 10 | +{ |
| 11 | + public enum PhoneType |
| 12 | + { |
| 13 | + Home, |
| 14 | + Work, |
| 15 | + Mobile, |
| 16 | + } |
| 17 | + |
| 18 | + public enum AddressType |
| 19 | + { |
| 20 | + Home, |
| 21 | + Work, |
| 22 | + Other, |
| 23 | + } |
| 24 | + |
| 25 | + public class Address |
| 26 | + { |
| 27 | + public string Line1 { get; set; } |
| 28 | + public string Line2 { get; set; } |
| 29 | + public string ZipCode { get; set; } |
| 30 | + public string State { get; set; } |
| 31 | + public string City { get; set; } |
| 32 | + public string Country { get; set; } |
| 33 | + } |
| 34 | + |
| 35 | + public class Customer |
| 36 | + { |
| 37 | + public Customer() |
| 38 | + { |
| 39 | + this.PhoneNumbers = new Dictionary<PhoneType, string>(); |
| 40 | + this.Addresses = new Dictionary<AddressType, Address>(); |
| 41 | + } |
| 42 | + |
| 43 | + [AutoIncrement] // Creates Auto primary key |
| 44 | + public int Id { get; set; } |
| 45 | + |
| 46 | + public string FirstName { get; set; } |
| 47 | + |
| 48 | + public string LastName { get; set; } |
| 49 | + |
| 50 | + [Index(Unique = true)] // Creates Unique Index |
| 51 | + public string Email { get; set; } |
| 52 | + |
| 53 | + public Dictionary<PhoneType, string> PhoneNumbers { get; set; } |
| 54 | + |
| 55 | + public Dictionary<AddressType, Address> Addresses { get; set; } |
| 56 | + |
| 57 | + public DateTime CreatedAt { get; set; } |
| 58 | + } |
| 59 | + |
| 60 | + public class Order |
| 61 | + { |
| 62 | + [AutoIncrement] |
| 63 | + public int Id { get; set; } |
| 64 | + |
| 65 | + [References(typeof(Customer))] //Creates Foreign Key |
| 66 | + public int CustomerId { get; set; } |
| 67 | + |
| 68 | + [References(typeof(Employee))] //Creates Foreign Key |
| 69 | + public int EmployeeId { get; set; } |
| 70 | + |
| 71 | + public Address ShippingAddress { get; set; } //Blobbed (no Address table) |
| 72 | + |
| 73 | + public DateTime? OrderDate { get; set; } |
| 74 | + |
| 75 | + public DateTime? RequiredDate { get; set; } |
| 76 | + |
| 77 | + public DateTime? ShippedDate { get; set; } |
| 78 | + |
| 79 | + public int? ShipVia { get; set; } |
| 80 | + |
| 81 | + public decimal Freight { get; set; } |
| 82 | + |
| 83 | + public decimal Total { get; set; } |
| 84 | + } |
| 85 | + |
| 86 | + public class OrderDetail |
| 87 | + { |
| 88 | + [AutoIncrement] |
| 89 | + public int Id { get; set; } |
| 90 | + |
| 91 | + [References(typeof(Order))] //Creates Foreign Key |
| 92 | + public int OrderId { get; set; } |
| 93 | + |
| 94 | + public int ProductId { get; set; } |
| 95 | + |
| 96 | + public decimal UnitPrice { get; set; } |
| 97 | + |
| 98 | + public short Quantity { get; set; } |
| 99 | + |
| 100 | + public decimal Discount { get; set; } |
| 101 | + } |
| 102 | + |
| 103 | + public class Employee |
| 104 | + { |
| 105 | + public int Id { get; set; } |
| 106 | + |
| 107 | + public string Name { get; set; } |
| 108 | + } |
| 109 | + |
| 110 | + public class Product |
| 111 | + { |
| 112 | + public int Id { get; set; } |
| 113 | + |
| 114 | + public string Name { get; set; } |
| 115 | + |
| 116 | + public decimal UnitPrice { get; set; } |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Stand-alone class, No other configs, nothing but POCOs. |
| 121 | + /// </summary> |
| 122 | + [TestFixture] |
| 123 | + public class CustomerOrdersUseCase : OrmLiteTestBase |
| 124 | + { |
| 125 | + [Test] |
| 126 | + public void Can_run_Customer_Orders_UseCase() |
| 127 | + { |
| 128 | + LogManager.LogFactory = new ConsoleLogFactory(); |
| 129 | + |
| 130 | + using (IDbConnection db = OpenDbConnection()) |
| 131 | + { |
| 132 | + //Re-Create all table schemas: |
| 133 | + RecreateTables(db); |
| 134 | + |
| 135 | + db.Insert(new Employee { Id = 1, Name = "Employee 1" }); |
| 136 | + db.Insert(new Employee { Id = 2, Name = "Employee 2" }); |
| 137 | + var product1 = new Product { Id = 1, Name = "Product 1", UnitPrice = 10 }; |
| 138 | + var product2 = new Product { Id = 2, Name = "Product 2", UnitPrice = 20 }; |
| 139 | + db.Save(product1, product2); |
| 140 | + |
| 141 | + var customer = new Customer { |
| 142 | + FirstName = "Orm", |
| 143 | + LastName = "Lite", |
| 144 | + |
| 145 | + PhoneNumbers = |
| 146 | + { |
| 147 | + { PhoneType.Home, "555-1234" }, |
| 148 | + { PhoneType.Work, "1-800-1234" }, |
| 149 | + { PhoneType.Mobile, "818-123-4567" }, |
| 150 | + }, |
| 151 | + Addresses = |
| 152 | + { |
| 153 | + { AddressType.Work, new Address { Line1 = "1 Street", Country = "US", State = "NY", City = "New York", ZipCode = "10101" } }, |
| 154 | + }, |
| 155 | + CreatedAt = DateTime.UtcNow, |
| 156 | + }; |
| 157 | + |
| 158 | + var customerId = db.Insert(customer, selectIdentity: true); //Get Auto Inserted Id |
| 159 | + customer = db.Single<Customer>(new { customer.Email }); //Query |
| 160 | + Assert.That(customer.Id, Is.EqualTo(customerId)); |
| 161 | + |
| 162 | + //Direct access to System.Data.Transactions: |
| 163 | + using (IDbTransaction trans = db.OpenTransaction(IsolationLevel.ReadCommitted)) |
| 164 | + { |
| 165 | + var order = new Order { |
| 166 | + CustomerId = customer.Id, |
| 167 | + EmployeeId = 1, |
| 168 | + OrderDate = DateTime.UtcNow, |
| 169 | + Freight = 10.50m, |
| 170 | + ShippingAddress = new Address { Line1 = "3 Street", Country = "US", State = "NY", City = "New York", ZipCode = "12121" }, |
| 171 | + }; |
| 172 | + db.Save(order); //Inserts 1st time |
| 173 | + |
| 174 | + //order.Id populated on Save(). |
| 175 | + |
| 176 | + var orderDetails = new[] { |
| 177 | + new OrderDetail { |
| 178 | + OrderId = order.Id, |
| 179 | + ProductId = product1.Id, |
| 180 | + Quantity = 2, |
| 181 | + UnitPrice = product1.UnitPrice, |
| 182 | + }, |
| 183 | + new OrderDetail { |
| 184 | + OrderId = order.Id, |
| 185 | + ProductId = product2.Id, |
| 186 | + Quantity = 2, |
| 187 | + UnitPrice = product2.UnitPrice, |
| 188 | + Discount = .15m, |
| 189 | + } |
| 190 | + }; |
| 191 | + |
| 192 | + db.Save(orderDetails); |
| 193 | + |
| 194 | + order.Total = orderDetails.Sum(x => x.UnitPrice * x.Quantity * x.Discount) + order.Freight; |
| 195 | + |
| 196 | + db.Save(order); //Updates 2nd Time |
| 197 | + |
| 198 | + trans.Commit(); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + public static void RecreateTables(IDbConnection db) |
| 204 | + { |
| 205 | + DropTables(db); |
| 206 | + |
| 207 | + db.CreateTable<Employee>(); |
| 208 | + db.CreateTable<Product>(); |
| 209 | + db.CreateTable<Customer>(); |
| 210 | + db.CreateTable<Order>(); |
| 211 | + db.CreateTable<OrderDetail>(); |
| 212 | + } |
| 213 | + |
| 214 | + public static void DropTables(IDbConnection db) |
| 215 | + { |
| 216 | + db.DropTable<OrderDetail>(); |
| 217 | + db.DropTable<Order>(); |
| 218 | + db.DropTable<Customer>(); |
| 219 | + db.DropTable<Product>(); |
| 220 | + db.DropTable<Employee>(); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | +} |
0 commit comments