public void Can_GetById() { Models.Data.ConsumableUsage usage1; var id1 = Guid.NewGuid(); using (var conn = factory.OpenDbConnection()) { usage1 = new Models.Data.ConsumableUsage { Id = id1, Date = DateTime.Today.AddDays(-1), Total = 10000 }; using (var tx = conn.OpenTransaction()) { conn.InsertParam(usage1); tx.Commit(); } } var usage = service.Get(id1); Compare(usage1, null, usage); }
public void Can_SearchByDate() { Models.Data.ConsumableUsage usage1, usage2; using (var conn = factory.OpenDbConnection()) { var id1 = Guid.NewGuid(); var id2 = Guid.NewGuid(); usage1 = new Models.Data.ConsumableUsage { Id = id1, Date = DateTime.Today.AddDays(-1), Total = 10000 }; usage2 = new Models.Data.ConsumableUsage() { Id = id2, Date = DateTime.Today, Total = 5000 }; using (var tx = conn.OpenTransaction()) { conn.InsertParam(usage1); conn.InsertParam(usage2); tx.Commit(); } } var searchInfo = new ConsumableUsageSearchInfo { Start = DateTime.Today.AddDays(-2), End = DateTime.Today.AddDays(1), PageIndex = 1, PageSize = 1 }; var result = service.Search(searchInfo); Assert.Equal(2, result.Total); Assert.Equal(usage2.Id, result.Items[0].Id); }
void Compare(Models.Data.ConsumableUsage data, IList <Models.Data.ConsumableUsageDetail> details, Models.ConsumableUsage model) { Assert.Equal(data.Id, model.Id); Assert.Equal(data.Total, model.Total); Assert.Equal(data.Date, model.Date); if (details != null) { foreach (var detail in details) { var detailModel = model.Details.FirstOrDefault( d => d.ConsumableId == detail.ConsumableId && d.HouseId == detail.HouseId); Assert.Equal(detail.ConsumableId, detailModel.ConsumableId); Assert.Equal(detail.Count, detailModel.Count); Assert.Equal(detail.HouseId, detailModel.HouseId); Assert.Equal(detail.SubTotal, detailModel.SubTotal); Assert.Equal(detail.UnitPrice, detailModel.UnitPrice); } } }
public void Can_GetByDate() { Models.Data.ConsumableUsage usage1 = null; using (var conn = factory.OpenDbConnection()) { usage1 = new Models.Data.ConsumableUsage { Id = Guid.NewGuid(), Date = DateTime.Today.AddDays(-1), Total = 10000 }; using (var tx = conn.OpenTransaction()) { conn.InsertParam(usage1); tx.Commit(); } } var usage = service.GetByDate(usage1.Date); Compare(usage1, null, usage); }
public void Can_DeleteById() { var usage = new Models.Data.ConsumableUsage { Id = Guid.NewGuid(), Date = DateTime.Today.AddDays(-1), Total = 10000 }; using (var conn = factory.OpenDbConnection()) { conn.InsertParam(usage); } service.Delete(usage.Id); using (var conn = factory.OpenDbConnection()) { var usageData = conn.GetByIdOrDefault<Models.Data.ConsumableUsage>(usage.Id); Assert.Null(usageData); } }
public void Can_DeleteById() { var usage = new Models.Data.ConsumableUsage { Id = Guid.NewGuid(), Date = DateTime.Today.AddDays(-1), Total = 10000 }; using (var conn = factory.OpenDbConnection()) { conn.InsertParam(usage); } service.Delete(usage.Id); using (var conn = factory.OpenDbConnection()) { var usageData = conn.GetByIdOrDefault <Models.Data.ConsumableUsage>(usage.Id); Assert.Null(usageData); } }
/// <summary> /// Maps the usage to model. Need to use automapper? /// </summary> /// <param name="usage">The usage.</param> /// <param name="details">The details.</param> /// <returns>EggFarmSystem.Models.ConsumableUsage.</returns> private ConsumableUsage MapUsageToModel(Models.Data.ConsumableUsage usage, List <Models.Data.ConsumableUsageDetail> details) { var model = new ConsumableUsage(); model.Id = usage.Id; model.Date = usage.Date; model.Total = usage.Total; model.Details = new List <ConsumableUsageDetail>(); foreach (var detail in details) { var detailModel = new ConsumableUsageDetail { ConsumableId = detail.ConsumableId, Count = detail.Count, HouseId = detail.HouseId, SubTotal = detail.SubTotal, UnitPrice = detail.UnitPrice }; model.Details.Add(detailModel); } return(model); }
public void Can_SearchByDate() { Models.Data.ConsumableUsage usage1, usage2; using (var conn = factory.OpenDbConnection()) { var id1 = Guid.NewGuid(); var id2 = Guid.NewGuid(); usage1 = new Models.Data.ConsumableUsage { Id = id1, Date = DateTime.Today.AddDays(-1), Total = 10000 }; usage2 = new Models.Data.ConsumableUsage() { Id = id2, Date = DateTime.Today, Total = 5000 }; using (var tx = conn.OpenTransaction()) { conn.InsertParam(usage1); conn.InsertParam(usage2); tx.Commit(); } } var searchInfo = new ConsumableUsageSearchInfo { Start = DateTime.Today.AddDays(-2), End = DateTime.Today.AddDays(1), PageIndex = 1, PageSize = 1 }; var result = service.Search(searchInfo); Assert.Equal(2, result.Total); Assert.Equal(usage2.Id, result.Items[0].Id); }
public void Can_SaveExistingConsumable() { var consumable = new Consumable { Id = Guid.NewGuid(), Name = "Zat 1", Type = (byte)ConsumableType.Feed, Unit = "liter", UnitPrice = 10000 }; var house = new HenHouse { Id = Guid.NewGuid(), Name = "House 1", Depreciation = 100, Active = true, PurchaseCost = 100000 }; var usage = new Models.Data.ConsumableUsage { Id = Guid.NewGuid(), Date = DateTime.Today.AddDays(-1), Total = 10000 }; using (var conn = factory.OpenDbConnection()) { conn.InsertParam(house); conn.InsertParam(consumable); conn.InsertParam(usage); } var model = new Models.ConsumableUsage { Id = usage.Id, Total = 12000, Date = DateTime.Today, Details = new List<ConsumableUsageDetail> { new ConsumableUsageDetail { ConsumableId = consumable.Id, Count = 10, HouseId = house.Id, SubTotal = 10000, UnitPrice = 100000 } } }; service.Save(model); using (var conn = factory.OpenDbConnection()) { var usageData = conn.GetById<Models.Data.ConsumableUsage>(usage.Id.ToString()); var details = conn.Where<Models.Data.ConsumableUsageDetail>(new { UsageId = usageData.Id }); Compare(usageData, details, model); } }
/// <summary> /// Saves the specified model. /// </summary> /// <param name="model">The model.</param> /// <returns><c>true</c> if save success, <c>false</c> otherwise</returns> public void Save(ConsumableUsage model) { using (var db = factory.OpenDbConnection()) { using (var trans = db.OpenTransaction()) { bool isNew = model.IsNew; Models.Data.ConsumableUsage usage = null; if (isNew) { usage = db.FirstOrDefault <Models.Data.ConsumableUsage>(u => u.Date == model.Date); } else { usage = db.Query <Models.Data.ConsumableUsage>("Date = @Date and Id <> @Id", new { Date = model.Date, Id = model.Id.ToString() }) .FirstOrDefault(); } if (usage != null) { trans.Rollback(); throw new ServiceException("Usage_DuplicateDate"); } if (isNew) { model.Id = Guid.NewGuid(); } usage = Mapper.Map <ConsumableUsage, Models.Data.ConsumableUsage>(model); if (isNew) { db.InsertParam(usage); } else { db.UpdateParam(usage); } if (!isNew) { db.Delete <Models.Data.ConsumableUsageDetail>(where : "UsageId = {0}".Params(usage.Id.ToString())); } foreach (var detailModel in model.Details) { var detail = Mapper.Map <ConsumableUsageDetail, Models.Data.ConsumableUsageDetail>(detailModel); detail.UsageId = usage.Id; db.InsertParam(detail); } try { trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw new ServiceException(ex.Message); } } } }
public void Can_SaveExistingConsumable() { var consumable = new Consumable { Id = Guid.NewGuid(), Name = "Zat 1", Type = (byte)ConsumableType.Feed, Unit = "liter", UnitPrice = 10000 }; var house = new HenHouse { Id = Guid.NewGuid(), Name = "House 1", Depreciation = 100, Active = true, PurchaseCost = 100000 }; var usage = new Models.Data.ConsumableUsage { Id = Guid.NewGuid(), Date = DateTime.Today.AddDays(-1), Total = 10000 }; using (var conn = factory.OpenDbConnection()) { conn.InsertParam(house); conn.InsertParam(consumable); conn.InsertParam(usage); } var model = new Models.ConsumableUsage { Id = usage.Id, Total = 12000, Date = DateTime.Today, Details = new List <ConsumableUsageDetail> { new ConsumableUsageDetail { ConsumableId = consumable.Id, Count = 10, HouseId = house.Id, SubTotal = 10000, UnitPrice = 100000 } } }; service.Save(model); using (var conn = factory.OpenDbConnection()) { var usageData = conn.GetById <Models.Data.ConsumableUsage>(usage.Id.ToString()); var details = conn.Where <Models.Data.ConsumableUsageDetail>(new { UsageId = usageData.Id }); Compare(usageData, details, model); } }