Пример #1
0
        public async Task<IActionResult> Create(int? id)
        {
            var currentUser = await this.userManager.GetUserAsync(this.User);

            var inputModel = new CreateHarvestInputModel
            {
                DateOfHarves = DateTime.Now.Date,
            };

            if (id == null)
            {
                inputModel.Apiaries = this.apiaryService.GetUserApiariesAsKeyValuePairs(currentUser.Id);
            }
            else
            {
                var apiaryNumber = this.apiaryService.GetApiaryNumberByBeehiveId(id.Value);
                var beehiveNumber = this.beehiveService.GetBeehiveNumberById(id.Value);

                inputModel.ApiaryId = this.apiaryService.GetApiaryIdByBeehiveId(id.Value);
                inputModel.BeehiveId = id.Value;
                inputModel.ApiaryNumber = apiaryNumber;
                inputModel.BeehiveNumber = beehiveNumber;
            }

            return this.View(inputModel);
        }
Пример #2
0
        public async Task<IActionResult> Create(CreateHarvestInputModel inputModel)
        {
            var currentUser = await this.userManager.GetUserAsync(this.User);

            if (!this.ModelState.IsValid)
            {
                inputModel.DateOfHarves = DateTime.UtcNow.Date;
                if (inputModel.BeehiveId == null)
                {
                    inputModel.Apiaries = this.apiaryService.GetUserApiariesAsKeyValuePairs(currentUser.Id);
                }

                return this.View(inputModel);
            }

            var apiaryOwnerId = this.apiaryService.GetApiaryOwnerIdByApiaryId(inputModel.ApiaryId);

            if (inputModel.BeehiveId == null)
            {
                var apiaryBeehives = this.beehiveService.GetBeehivesByApiaryId<BeehiveDataModel>(inputModel.ApiaryId).ToList();
                if (inputModel.AllBeehives)
                {
                    var beehiveIds = apiaryBeehives.Select(b => b.Id).ToList();
                    await this.harvestService.CreateUserHarvestAsync(apiaryOwnerId, currentUser.Id, inputModel, beehiveIds);
                }
                else
                {
                    var selectedIds = new List<int>();
                    var selectedBeehivesNumbers = inputModel.BeehiveNumbersSpaceSeparated.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(n => Convert.ToInt32(n)).ToList();
                    foreach (var number in selectedBeehivesNumbers)
                    {
                        var beehive = apiaryBeehives.FirstOrDefault(b => b.Number == number);
                        if (beehive != null)
                        {
                            selectedIds.Add(beehive.Id);
                        }
                    }

                    await this.harvestService.CreateUserHarvestAsync(apiaryOwnerId, currentUser.Id, inputModel, selectedIds);
                }

                this.TempData[GlobalConstants.SuccessMessage] = $"Успешно добавен добив!";
                return this.RedirectToAction("Index", "Home");
            }
            else
            {
                await this.harvestService.CreateUserHarvestAsync(apiaryOwnerId, currentUser.Id, inputModel, new List<int> { inputModel.BeehiveId.Value });

                this.TempData[GlobalConstants.SuccessMessage] = $"Успешно добавен добив!";
                return this.RedirectToAction(nameof(this.AllByBeehiveId), new { id = inputModel.BeehiveId.Value });
            }
        }
Пример #3
0
        public async Task <int> CreateUserHarvestAsync(string ownerId, string creatorId, CreateHarvestInputModel inputModel, List <int> beehiveIds)
        {
            var harvest = new Harvest
            {
                OwnerId            = ownerId,
                CreatorId          = creatorId,
                HarvestName        = inputModel.HarvestName,
                DateOfHarves       = inputModel.DateOfHarves,
                Note               = inputModel.Note,
                HarvestProductType = inputModel.HarvestProductType,
                HoneyType          = inputModel.HoneyType,
                Quantity           = Convert.ToDouble(inputModel.QuantityText),
                Unit               = inputModel.Unit,
            };

            await this.harvestRepository.AddAsync(harvest);

            await this.harvestRepository.SaveChangesAsync();

            foreach (var id in beehiveIds)
            {
                var treatedBeehive = new HarvestedBeehive
                {
                    BeehiveId = id,
                    HarvestId = harvest.Id,
                };

                await this.harvestedBeehiveRepository.AddAsync(treatedBeehive);

                await this.harvestRepository.SaveChangesAsync();
            }

            return(harvest.Id);
        }