Example #1
0
        private HttpResponseMessage ProcessNewFeedRecord(HttpRequestMessage request, FeedingDTO uDto, string key, int companyId, int UserId)
        {
            var ur = new FeedingRepository();
            var o2 = new Feeding();
            var validationErrors = GetFeedValidationErrors(ur, o2, uDto, companyId, UserId);

            if (validationErrors.Any())
            {
                return(ProcessValidationErrors(request, validationErrors, key));
            }
            //  no validation errors...
            //Pond.CompanyId = companyId;
            o2 = ur.Save(o2);
            var farmid = uDto.FarmID;

            var br       = new BinRepository();
            var binCount = br.GetFarmBinList(farmid).Count();

            if (binCount > 0)
            {
                var binDisb = br.GetNewBinDisbursementRecord();
                binDisb.DateCreated = DateTime.Now;
                var disbType  = br.GetDisbursementType("Routine Feeding");
                var ticketNbr = br.GetLastBinLoadTicketNumber(uDto.BinID.Value);
                if (ticketNbr == 0)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                                                       string.Format("{0}{1}", "There are no Tickets in BinLoads for BinID ", uDto.BinID)));
                }

                var dto = new BinDisbursementDto()
                {
                    BinID            = uDto.BinID.Value,
                    TicketNumber     = ticketNbr,
                    Pounds           = int.Parse(uDto.PoundsFed),
                    Note             = "Record created from daily feed disbursement input screen",
                    DisbursementType = disbType,
                    DisbursementDate = DateTime.Now,
                    CreatedDate      = DateTime.Now,
                    UserID           = UserId,
                    FeedID           = o2.FeedingId
                };
                validationErrors = GetBinDisbursementErrors(br, binDisb, dto, companyId, UserId);
                if (validationErrors.Any())
                {
                    return(ProcessValidationErrors(request, validationErrors, key));
                }
                br.SaveChanges();
                br.UpdateBinCurrentPounds(null, binDisb);
            }

            uDto.Key = key;
            var response = request.CreateResponse(HttpStatusCode.Created, uDto);

            response.Headers.Location = new Uri(Url.Link("Default", new {
                id = o2.FeedingId
            }));
            return(response);
        }
Example #2
0
 private List <DbValidationError> GetBinDisbursementErrors(BinRepository pr, BinDisbursement bd, BinDisbursementDto dto, int companyId, int PondId)
 {
     bd.ProcessRecord(dto);
     return(pr.Validate(bd));
 }