public void Can_GetByDate()
 {
     DateTime today = DateTime.Today;
     var production = new EggProduction();
     serviceMock.Setup(s => s.GetByDate(today)).Returns(production);
     Assert.Equal(production, controller.GetByDate(today));
 }
 public void Can_GetById()
 {
     Guid id = Guid.NewGuid();
     var production= new EggProduction();
     serviceMock.Setup(s => s.Get(id)).Returns(production);
     Assert.Equal(production, controller.Get(id));
 }
        public HttpResponseMessage Post(EggProduction value)
        {
            HttpResponseMessage response = null;
            ValidateModel(value);

            if (!ModelState.IsValid)
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest, GetModelErrors());
                return response;
            }

            try
            {
                service.Save(value);
                response = Request.CreateResponse(HttpStatusCode.Created, value);
                string uri = Url.Link("DefaultApi", new { id = value.Id });
                response.Headers.Location = new Uri(uri);
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }

            return response;
        }
        public void Save(EggProduction production)
        {
            using (var db = factory.OpenDbConnection())
            {
                using (var tx = db.OpenTransaction())
                {
                    bool isNew = production.IsNew;

                    Models.Data.EggProduction productionData = null;

                    if (isNew)
                    {
                        productionData = db.FirstOrDefault<Models.Data.EggProduction>(u => u.Date == production.Date);
                    }
                    else
                    {
                        productionData = db.Query<Models.Data.EggProduction>("Date = @Date and Id <> @Id",
                                                                      new { Date = production.Date, Id = production.Id.ToString() })
                                     .FirstOrDefault();
                    }
                    if (productionData != null)
                    {
                        tx.Rollback();
                        throw new ServiceException("EggProduction_DuplicateDate");
                    }

                    if (isNew)
                        production.Id = Guid.NewGuid();

                    productionData = Mapper.Map<EggProduction, Models.Data.EggProduction>(production);

                    if (isNew) db.InsertParam(productionData); else db.UpdateParam(productionData);

                    if (!isNew)
                        db.Delete<Models.Data.EggProductionDetail>(where: "ProductionId = {0}".Params(productionData.Id.ToString()));

                    foreach (var detail in production.Details)
                    {
                        var detailData = Mapper.Map<EggProductionDetail, Models.Data.EggProductionDetail>(detail);
                        detailData.ProductionId = production.Id;
                        db.InsertParam(detailData);
                    }
                    try
                    {
                        tx.Commit();
                    }
                    catch (Exception ex)
                    {
                        tx.Rollback();
                        throw new ServiceException(ex.Message);
                    }
                }
            }
        }
 public void Post_ReturnsBadRequest_IfValueIsInvalid()
 {
     var production = new EggProduction();
     var response = controller.Post(production);
     Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
 }
 public void Put_SaveEmployeeCost()
 {
     var cost = new EggProduction
     {
         Id = Guid.NewGuid(),
         Date = DateTime.Today,
         Details = new List<EggProductionDetail>()
                 {
                     new EggProductionDetail
                         {
                             HouseId = Guid.NewGuid(),
                             CrackedEggCount = 1,
                             Fcr = 1,
                             GoodEggCount = 1,
                             RetailQuantity = 1
                         }
                 }
     };
     var response = controller.Put(cost.Id, cost);
     Assert.Equal(HttpStatusCode.OK, response.StatusCode);
 }
        public void Put_ReturnsInternalServerError_IfSavingFailed()
        {
            var cost = new EggProduction
            {
                Id = Guid.NewGuid(),
                Date = DateTime.Today,
                Details = new List<EggProductionDetail>()
                        {
                            new EggProductionDetail
                                {
                                    HouseId = Guid.NewGuid(),
                                    CrackedEggCount = 1,
                                    Fcr = 1,
                                    GoodEggCount = 1,
                                    RetailQuantity = 1
                                }
                        }
            };

            serviceMock.Setup(c => c.Save(cost)).Throws(new Exception());
            var response = controller.Put(cost.Id, cost);
            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
        }
 //TODO:
 //find a way to mock Url.Link
 //[Fact]
 public void Post_SaveEggProduction()
 {
     var production = new EggProduction
     {
         Date = DateTime.Today,
         Details = new List<EggProductionDetail>()
                 {
                     new EggProductionDetail
                         {
                             HouseId = Guid.NewGuid(),
                             CrackedEggCount = 1,
                             Fcr = 1,
                             GoodEggCount = 1,
                             RetailQuantity = 1
                         }
                 }
     };
     var response = controller.Post(production );
     Assert.Equal(HttpStatusCode.Created, response.StatusCode);
 }
        public HttpResponseMessage Put(Guid id, EggProduction value)
        {
            ValidateModel(value);

            if (!ModelState.IsValid || value.Id != id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, GetModelErrors());
            }

            HttpResponseMessage response = null;

            try
            {
                service.Save(value);
                response = Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }

            return response;
        }