Пример #1
0
        public async Task <HbResult <HbLaying> > Update(UpdateLayingForm form)
        {
            var laying = await _dc.Layings.FirstOrDefaultAsync(u => !u.IsDeleted && u.Id == form.Id);

            if (laying == null)
            {
                return(new HbResult <HbLaying>(ErrorCodes.LayingNotFound));
            }

            var lotExist = await _dc.Lots.Where(u => !u.IsDeleted && u.Id == form.LotId).AnyAsync();

            if (!lotExist)
            {
                return(new HbResult <HbLaying>(ErrorCodes.LotNotFound));
            }

            var incubatorExist = await _dc.Incubators.AnyAsync(u => !u.IsDeleted && u.Id == form.IncubatorId);

            if (!incubatorExist)
            {
                return(new HbResult <HbLaying>(ErrorCodes.IncubatorNotFound));
            }

            var purchasesEggs = await _dc.Purchases.Where(u => u.LotId == form.LotId && !u.IsDeleted).SumAsync(u => u.Count);

            var existLayings = await _dc.Layings.Where(u => u.LotId == form.LotId && u.Id != form.Id && !u.IsDeleted).SumAsync(u => u.Count);

            if (purchasesEggs - existLayings < form.Count)
            {
                return(new HbResult <HbLaying>(ErrorCodes.LayingCountMoreThanPurchasesCount));
            }


            laying.Count       = form.Count;
            laying.IncubatorId = form.IncubatorId;
            laying.LotId       = form.LotId;
            laying.LayingDate  = form.LayingDate;

            await _dc.SaveChangesAsync();

            await _lots.RecalculateLot(laying.LotId);

            return(new HbResult <HbLaying>(_mapper.Map <HbLaying>(laying)));
        }
Пример #2
0
        public async Task <IActionResult> Edit(UpdateLayingForm form)
        {
            if (!ModelState.IsValid)
            {
                await InitLotsList(form);

                return(View(form));
            }

            var res = await _laying.Update(form);

            if (res.IsCorrect)
            {
                return(RedirectToAction(nameof(List)));
            }

            ViewData[ViewDataKeys.ErrorMessage] = res.ErrorMessage;
            await InitLotsList(form);

            return(View(form));
        }