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); }
public HttpResponseMessage FeedAddOrEdit([FromBody] FeedingDTO uDto) { string key; var ur = new AppUserRepository(); var companyId = 0; var UserId = ur.ValidateUser(uDto.Key, out key, ref companyId); AppUserRoleRepository aur = new AppUserRoleRepository(); if (UserId > 0 && aur.IsInRole(UserId, "Chowtime")) { var thisuser = ur.GetById(UserId); var pr = new PondRepository(); int thisfarm = pr.GetById(int.Parse(uDto.PondId)).FarmId; int UsersFarmId = thisuser.UserFarms.Where(x => x.FarmId == thisfarm).SingleOrDefault().UserFarmId; uDto.UsersFarmId = UsersFarmId.ToString(); uDto.FarmID = thisfarm; var feed = new Feeding(); var errors = ValidateDtoData(uDto, feed); if (errors.Any()) { return(ProcessValidationErrors(Request, errors, key)); } var NEFeedingId = 0; if (int.TryParse(uDto.FeedingId, out NEFeedingId)) { if (NEFeedingId == -1) { // creating new Feeding record return(ProcessNewFeedRecord(Request, uDto, key, companyId, UserId)); } else { // editing existing Feeding record return(ProcessExistingFeedRecord(Request, uDto, NEFeedingId, key, companyId, UserId)); } } // no idea what this is var msg = "invalid data structure submitted"; return(Request.CreateResponse(HttpStatusCode.BadRequest, msg)); } var message = "validation failed"; return(Request.CreateResponse(HttpStatusCode.NotFound, message)); }
public HttpResponseMessage FeedById([FromBody] FeedingDTO uDto) { string key; var ur = new AppUserRepository(); var companyId = 0; var UserId = ur.ValidateUser(uDto.Key, out key, ref companyId); AppUserRoleRepository aur = new AppUserRoleRepository(); if (UserId > 0 && aur.IsInRole(UserId, "Chowtime")) { var O2r = new FeedingRepository(); var data = O2r.GetById(int.Parse(uDto.FeedingId)); var col = new Collection <Dictionary <string, string> >(); var dic = new Dictionary <string, string>(); dic.Add("PondId", data.PondId.ToString()); dic.Add("FeedingId", data.FeedingId.ToString()); dic.Add("FeedDate", data.FeedDate.ToString()); dic.Add("PoundsFed", data.PoundsFed.ToString()); col.Add(dic); var retVal = new GenericDTO { Key = key, ReturnData = col }; return(Request.CreateResponse(HttpStatusCode.OK, retVal)); } var message = "validation failed"; return(Request.CreateResponse(HttpStatusCode.NotFound, message)); }
private List <DbValidationError> GetFeedValidationErrors(FeedingRepository pr, Feeding contact, FeedingDTO cqDto, int companyId, int PondId) { contact.ProcessRecord(cqDto); return(pr.Validate(contact)); }
private HttpResponseMessage ProcessExistingFeedRecord(HttpRequestMessage request, FeedingDTO cqDto, int contactId, string key, int companyId, int UserId) { var o2r = new FeedingRepository(); var o2 = new Feeding(); o2 = o2r.GetById(contactId); // is the Pond eligible to update the prospect? var validationErrors = GetFeedValidationErrors(o2r, o2, cqDto, companyId, UserId); if (validationErrors.Any()) { return(ProcessValidationErrors(request, validationErrors, key)); } // no validation errors... var binDisb = o2r.GetBinDisbursement(o2.FeedingId); if (binDisb != null) { // there is a BinDisbursement record which has to be modified binDisb.Pounds = o2.PoundsFed; binDisb.UserID = UserId; o2r.SaveChanges(); var br = new BinRepository(); br.UpdateBinCurrentPounds(null, binDisb); } else { // there is no BinDisbursement record to mofify o2r.SaveChanges(); } cqDto.Key = key; return(request.CreateResponse(HttpStatusCode.Accepted, cqDto)); }