Ejemplo n.º 1
0
        public async Task <IActionResult> PutUser(long id, UserEditDTO userEditDTO)
        {
            var currentUserId = long.Parse(User.Identity.Name);

            if (id != currentUserId)
            {
                return(Forbid());
            }

            if (id != userEditDTO.Id)
            {
                return(BadRequest());
            }

            var user = await _context.Users.FindAsync(id);

            _mapper.Map(userEditDTO, user);
            if (!string.IsNullOrWhiteSpace(userEditDTO.Password))
            {
                _userService.CreateHashedPassword(user, userEditDTO.Password);
            }
            await _context.SaveChangesAsync();

            return(NoContent());
        }
        public async Task <IActionResult> EditWorker(long farmId, long userId, string permissions)
        {
            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, farmId);

            if (farmWorker == null || farmWorker.Role != WorkerRole.Owner)
            {
                return(Forbid());
            }

            var farm = await _context.Farms.FindAsync(farmId);

            if (farm == null)
            {
                return(NotFound());
            }

            var farmWorkerToUpdate = await _context.FarmWorkers.FindAsync(userId, farmId);

            if (farmWorkerToUpdate == null || farmWorkerToUpdate == farmWorker)
            {
                return(NotFound());
            }

            farmWorkerToUpdate.Permissions = permissions;
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <BeehiveReadDTO> > CreateBeehive(BeehiveCreateDTO beehiveCreateDTO)
        {
            var farm = await _context.Farms.FindAsync(beehiveCreateDTO.FarmId);

            if (farm == null)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, farm.Id);

            if (farmWorker == null || farmWorker.Permissions[3] != '1')
            {
                return(Forbid());
            }

            var beehive = _mapper.Map <Beehive>(beehiveCreateDTO);

            if (!IsBeehiveDataCorrect(beehive.Type, beehive))
            {
                return(BadRequest("Incorrect data"));
            }

            _context.Beehives.Add(beehive);
            await _context.SaveChangesAsync();

            var beehiveReadDTO = _mapper.Map <BeehiveReadDTO>(beehive);

            return(CreatedAtAction(nameof(GetBeehive), new { id = beehive.Id }, beehiveReadDTO));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <FeedingReadDTO> > CreateFeeding(FeedingCreateDTO feedingCreateDTO)
        {
            var beefamily = await _context.BeeFamilies.FindAsync(feedingCreateDTO.BeeFamilyId);

            if (beefamily == null)
            {
                return(BadRequest());
            }

            var food = await _context.Foods.FindAsync(feedingCreateDTO.FoodId);

            if (food == null || beefamily.FarmId != food.FarmId)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, beefamily.FarmId);

            if (farmWorker == null || farmWorker.Permissions[27] != '1')
            {
                return(Forbid());
            }


            var feeding = _mapper.Map <Feeding>(feedingCreateDTO);

            _context.Feedings.Add(feeding);
            await _context.SaveChangesAsync();

            var feedingReadDTO = _mapper.Map <FeedingReadDTO>(feeding);

            return(CreatedAtAction("GetFeeding", "Feedings", new { id = feeding.Id }, feedingReadDTO));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <FoodReadDTO> > CreateFood(FoodCreateDTO foodCreateDTO)
        {
            var farm = await _context.Farms.FindAsync(foodCreateDTO.FarmId);

            if (farm == null)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, farm.Id);

            if (farmWorker == null || farmWorker.Permissions[27] != '1')
            {
                return(Forbid());
            }


            var food = _mapper.Map <Food>(foodCreateDTO);

            _context.Foods.Add(food);
            await _context.SaveChangesAsync();

            var foodReadDTO = _mapper.Map <FoodReadDTO>(food);

            return(CreatedAtAction("GetFood", "Foods", new { id = food.Id }, foodReadDTO));
        }
Ejemplo n.º 6
0
        public async Task <ActionResult <ApiaryReadDTO> > CreateApiary(ApiaryCreateDTO apiaryCreateDTO)
        {
            var farm = await _context.Farms.FindAsync(apiaryCreateDTO.FarmId);

            if (farm == null)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, farm.Id);

            if (farmWorker == null || farmWorker.Role != WorkerRole.Owner)
            {
                return(Forbid());
            }

            var apiary = _mapper.Map <Apiary>(apiaryCreateDTO);

            _context.Apiaries.Add(apiary);
            await _context.SaveChangesAsync();

            var apiaryReadDTO = _mapper.Map <ApiaryReadDTO>(apiary);

            return(CreatedAtAction("GetApiary", "Apiaries", new { id = apiary.Id }, apiaryReadDTO));
        }
        public async Task <ActionResult <ManufacturerReadDTO> > CreateManufacturer(Manufacturer manufacturer)
        {
            _context.Manufacturers.Add(manufacturer);
            await _context.SaveChangesAsync();

            var manufacturerReadDTO = _mapper.Map <ManufacturerReadDTO>(manufacturer);

            return(CreatedAtAction(nameof(GetManufacturer), new { id = manufacturer.Id }, manufacturerReadDTO));
        }
        public async Task <ActionResult <TodoItemReadDTO> > CreateTodoItem(TodoItemCreateDTO todoItemCreateDTO)
        {
            var farm = await _context.Farms.FindAsync(todoItemCreateDTO.FarmId);

            if (farm == null)
            {
                return(BadRequest());
            }

            if (todoItemCreateDTO.ApiaryId != null && todoItemCreateDTO.BeeFamilyId == null)
            {
                var apiary = await _context.Apiaries.FindAsync(todoItemCreateDTO.ApiaryId);

                if (apiary == null || apiary.FarmId != farm.Id)
                {
                    return(BadRequest());
                }
            }
            else if (todoItemCreateDTO.BeeFamilyId != null && todoItemCreateDTO.ApiaryId == null)
            {
                var beehive = await _context.BeeFamilies.FindAsync(todoItemCreateDTO.BeeFamilyId);

                if (beehive == null || beehive.FarmId != farm.Id)
                {
                    return(BadRequest());
                }
            }
            else if (todoItemCreateDTO.BeeFamilyId != null && todoItemCreateDTO.ApiaryId != null)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, farm.Id);

            if (farmWorker == null || farmWorker.Permissions[24] != '1')
            {
                return(Forbid());
            }

            var todoItem = _mapper.Map <TodoItem>(todoItemCreateDTO);

            _context.TodoItems.Add(todoItem);
            await _context.SaveChangesAsync();

            var todoItemReadDTO = _mapper.Map <TodoItemReadDTO>(todoItem);

            return(CreatedAtAction("GetTodoItem", "TodoItems", new { id = todoItem.Id }, todoItemReadDTO));
        }
Ejemplo n.º 9
0
        public async Task <ActionResult <HarvestReadDTO> > CreateHarvest(HarvestCreateDTO harvestCreateDTO)
        {
            var farm = await _context.Farms.FindAsync(harvestCreateDTO.FarmId);

            if (farm == null)
            {
                return(BadRequest());
            }

            if (harvestCreateDTO.ApiaryId != null && harvestCreateDTO.BeeFamilyId == null)
            {
                var apiary = await _context.Apiaries.FindAsync(harvestCreateDTO.ApiaryId);

                if (apiary == null || apiary.FarmId != farm.Id)
                {
                    return(BadRequest());
                }
            }
            else if (harvestCreateDTO.BeeFamilyId != null && harvestCreateDTO.ApiaryId == null)
            {
                var beehive = await _context.BeeFamilies.FindAsync(harvestCreateDTO.BeeFamilyId);

                if (beehive == null || beehive.FarmId != farm.Id)
                {
                    return(BadRequest());
                }
            }
            else if (harvestCreateDTO.BeeFamilyId != null && harvestCreateDTO.ApiaryId != null)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, farm.Id);

            if (farmWorker == null || farmWorker.Permissions[9] != '1')
            {
                return(Forbid());
            }

            var harvest = _mapper.Map <Harvest>(harvestCreateDTO);

            _context.Harvests.Add(harvest);
            await _context.SaveChangesAsync();

            var harvestReadDTO = _mapper.Map <HarvestReadDTO>(harvest);

            return(CreatedAtAction("GetHarvest", "Harvests", new { id = harvest.Id }, harvestReadDTO));
        }
Ejemplo n.º 10
0
        public async Task <ActionResult <FarmReadDTO> > CreateFarm(FarmCreateDTO farmCreateDTO)
        {
            var currentUserId = long.Parse(User.Identity.Name);
            var farm          = _mapper.Map <Farm>(farmCreateDTO);

            _context.Farms.Add(farm);
            await _context.SaveChangesAsync();

            var farmWorkersList = await _context.FarmWorkers.Where(l => l.UserId == currentUserId).ToListAsync();

            if (!farmWorkersList.Any())
            {
                var user = await _context.Users.FindAsync(currentUserId);

                user.DefaultFarmId         = farm.Id;
                _context.Entry(user).State = EntityState.Modified;
                await _context.SaveChangesAsync();
            }

            var farmWorker = new FarmWorker
            {
                Role        = WorkerRole.Owner,
                FarmId      = farm.Id,
                UserId      = currentUserId,
                Permissions = "111111111111111111111111111111"
            };

            _context.FarmWorkers.Add(farmWorker);
            await _context.SaveChangesAsync();

            var farmReadDTO = _mapper.Map <FarmReadDTO>(farm);

            return(CreatedAtAction("GetFarm", "Farms", new { id = farm.Id }, farmReadDTO));
        }
Ejemplo n.º 11
0
        public async Task <ActionResult <QueensRaisingReadDTO> > CreateQueensRaising(QueensRaisingCreateDTO queensRaisingCreateDTO)
        {
            var queen = await _context.Queens.FindAsync(queensRaisingCreateDTO.MotherId);

            if (queen == null)
            {
                return(BadRequest());
            }

            var beefamily = await _context.BeeFamilies.FindAsync(queensRaisingCreateDTO.BeefamilyId);

            if (beefamily == null || queen.FarmId != beefamily.FarmId)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, queen.FarmId);

            if (farmWorker == null || farmWorker.Permissions[21] != '1')
            {
                return(Forbid());
            }

            //Only living queen can be mother
            if (queen.State != QueenState.GyvenaAvilyje)
            {
                return(BadRequest());
            }

            if (!IsDevelopmentPlaceValid(queensRaisingCreateDTO.DevelopmentPlace ?? DevelopmentPlace.Beehive))
            {
                return(BadRequest("Invalid queens development place"));
            }

            var queensRaising = _mapper.Map <QueensRaising>(queensRaisingCreateDTO);

            _context.QueensRaisings.Add(queensRaising);
            await _context.SaveChangesAsync();

            var queensRaisingReadDTO = _mapper.Map <QueensRaisingReadDTO>(queensRaising);

            return(CreatedAtAction("GetQueensRaising", "QueensRaisings", new { id = queensRaising.Id }, queensRaisingReadDTO));
        }
        public async Task <ActionResult <InvitationReadDTO> > GetFarmInvitation(long farmId)
        {
            var farm = await _context.Farms.FindAsync(farmId);

            if (farm == null)
            {
                return(NotFound());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, farmId);

            if (farmWorker == null || farmWorker.Role != WorkerRole.Owner)
            {
                return(Forbid());
            }

            var invitation = await _context.Invitations.FindAsync(farmId);

            if (invitation == null)
            {
                var invitationToSave = new Invitation
                {
                    ExpirationDate = DateTime.UtcNow.AddMinutes(10),
                    Code           = Guid.NewGuid(),
                    FarmId         = farmId
                };
                await _context.Invitations.AddAsync(invitationToSave);

                await _context.SaveChangesAsync();

                invitation = await _context.Invitations.FindAsync(farmId);
            }
            else if (invitation.ExpirationDate < DateTime.UtcNow)
            {
                //edit old one
                invitation.Code           = Guid.NewGuid();
                invitation.ExpirationDate = DateTime.UtcNow.AddMinutes(10);
                await _context.SaveChangesAsync();
            }

            return(_mapper.Map <InvitationReadDTO>(invitation));
        }
        public async Task <ActionResult <BeehiveBeeFamilyReadDTO> > CreateBeehiveBeeFamily(BeehiveBeeFamilyCreateDTO beehiveBeeFamilyCreateDTO)
        {
            var beehiveBeeFamilyDuplicate = _context.BeehiveBeeFamilies.FirstOrDefault(bb =>
                                                                                       (bb.BeeFamilyId == beehiveBeeFamilyCreateDTO.BeeFamilyId && bb.DepartDate == null) ||
                                                                                       (bb.BeehiveId == beehiveBeeFamilyCreateDTO.BeehiveId && bb.DepartDate == null));

            if (beehiveBeeFamilyDuplicate != null)
            {
                return(BadRequest());
            }

            var beehive = await _context.Beehives.FindAsync(beehiveBeeFamilyCreateDTO.BeehiveId);

            if (beehive == null)
            {
                return(BadRequest());
            }

            var beeFamily = await _context.BeeFamilies.FindAsync(beehiveBeeFamilyCreateDTO.BeeFamilyId);

            if (beeFamily == null || beehive.FarmId != beeFamily.FarmId)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, beehive.FarmId);

            if (farmWorker == null || farmWorker.Permissions[0] != '1')
            {
                return(Forbid());
            }

            var beehiveBeeFamily = _mapper.Map <BeehiveBeeFamily>(beehiveBeeFamilyCreateDTO);

            _context.BeehiveBeeFamilies.Add(beehiveBeeFamily);
            await _context.SaveChangesAsync();

            var beehiveBeeFamilyReadDTO = _mapper.Map <BeehiveBeeFamilyReadDTO>(beehiveBeeFamily);

            return(CreatedAtAction(nameof(GetOneBeehiveBeeFamily), new { id = beehiveBeeFamily.Id }, beehiveBeeFamilyReadDTO));
        }
        public async Task <ActionResult <ApiaryBeeFamilyReadDTO> > CreateApiaryBeeFamily(ApiaryBeeFamilyCreateDTO apiaryBeeFamilyCreateDTO)
        {
            var beeFamilyApiaryDuplicate = _context.ApiaryBeeFamilies.FirstOrDefault(ab =>
                                                                                     ab.BeeFamilyId == apiaryBeeFamilyCreateDTO.BeeFamilyId &&
                                                                                     ab.DepartDate == null);

            if (beeFamilyApiaryDuplicate != null)
            {
                return(BadRequest());
            }

            var apiary = await _context.Apiaries.FindAsync(apiaryBeeFamilyCreateDTO.ApiaryId);

            if (apiary == null)
            {
                return(BadRequest());
            }

            var beeFamily = await _context.BeeFamilies.FindAsync(apiaryBeeFamilyCreateDTO.BeeFamilyId);

            if (beeFamily == null || apiary.FarmId != beeFamily.FarmId)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, apiary.FarmId);

            if (farmWorker == null || farmWorker.Permissions[0] != '1')
            {
                return(Forbid());
            }

            var apiaryBeehive = _mapper.Map <ApiaryBeeFamily>(apiaryBeeFamilyCreateDTO);

            _context.ApiaryBeeFamilies.Add(apiaryBeehive);
            await _context.SaveChangesAsync();

            var apiaryBeehiveReadDTO = _mapper.Map <ApiaryBeeFamilyReadDTO>(apiaryBeehive);

            return(CreatedAtAction(nameof(GetOneApiaryBeeFamily), new { id = apiaryBeehive.Id }, apiaryBeehiveReadDTO));
        }
Ejemplo n.º 15
0
        public async Task <ActionResult <BeeFamilyQueenReadDTO> > CreateBeefamilyQueen(BeeFamilyQueenCreateDTO beefamilyQueenCreateDTO)
        {
            var beefamilyQueenDuplicate = _context.BeeFamilyQueens.FirstOrDefault(fq =>
                                                                                  (fq.BeeFamilyId == beefamilyQueenCreateDTO.BeeFamilyId && fq.TakeOutDate == null) ||
                                                                                  (fq.QueenId == beefamilyQueenCreateDTO.QueenId && fq.TakeOutDate == null));

            if (beefamilyQueenDuplicate != null)
            {
                return(BadRequest());
            }

            var queen = await _context.Queens.FindAsync(beefamilyQueenCreateDTO.QueenId);

            if (queen == null)
            {
                return(BadRequest());
            }

            var beeFamily = await _context.BeeFamilies.FindAsync(beefamilyQueenCreateDTO.BeeFamilyId);

            if (beeFamily == null || queen.FarmId != beeFamily.FarmId)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, queen.FarmId);

            if (farmWorker == null || farmWorker.Permissions[18] != '1')
            {
                return(Forbid());
            }

            var beefamilyQueen = _mapper.Map <BeefamilyQueen>(beefamilyQueenCreateDTO);

            _context.BeeFamilyQueens.Add(beefamilyQueen);
            await _context.SaveChangesAsync();

            var beefamilyQueenReadDTO = _mapper.Map <BeeFamilyQueenReadDTO>(beefamilyQueen);

            return(CreatedAtAction(nameof(GetBeeFamilyQueen), new { id = beefamilyQueen.Id }, beefamilyQueenReadDTO));
        }
Ejemplo n.º 16
0
        public async Task <ActionResult <QueenReadDTO> > CreateQueen(QueenCreateDTO queenCreateDTO)
        {
            var farm = await _context.Farms.FindAsync(queenCreateDTO.FarmId);

            if (farm == null)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, farm.Id);

            if (farmWorker == null || farmWorker.Permissions[18] != '1')
            {
                return(Forbid());
            }

            if (!IsQueenStateValid(queenCreateDTO.State ?? 0) || !IsCreatingState(queenCreateDTO.State ?? 0))
            {
                return(BadRequest());
            }

            if (queenCreateDTO.State == QueenState.Lopšys)
            {
                queenCreateDTO.IsFertilized   = false;
                queenCreateDTO.MarkingColor   = null;
                queenCreateDTO.HatchingDate   = null;
                queenCreateDTO.BroodStartDate = null;
            }

            var queen = _mapper.Map <Queen>(queenCreateDTO);

            _context.Queens.Add(queen);
            await _context.SaveChangesAsync();

            var queenReadDTO = _mapper.Map <QueenReadDTO>(queen);

            return(CreatedAtAction("GetQueen", "Queens", new { id = queen.Id }, queenReadDTO));
        }
Ejemplo n.º 17
0
        public async Task <ActionResult <NestReductionReadDTO> > CreateNestReduction(NestReductionCreateDTO nestReductionCreateDTO)
        {
            var beefamily = await _context.BeeFamilies.FindAsync(nestReductionCreateDTO.BeefamilyId);

            if (beefamily == null || beefamily.State == BeeFamilyStates.Išmirusi || beefamily.State == BeeFamilyStates.SujungtaSuKitaŠeima)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, beefamily.FarmId);

            if (farmWorker == null || farmWorker.Permissions[15] != '1')
            {
                return(Forbid());
            }

            var beehiveBeefamily = await _context.BeehiveBeeFamilies.FirstOrDefaultAsync(bb => bb.BeeFamilyId == beefamily.Id &&
                                                                                         bb.DepartDate == null);

            if (beehiveBeefamily == null)
            {
                return(BadRequest());
            }
            var beehive = await _context.Beehives.FindAsync(beehiveBeefamily.BeehiveId);

            if (beehive == null)
            {
                return(BadRequest());
            }
            var nestReduction = _mapper.Map <NestReduction>(nestReductionCreateDTO);

            //If new nest reduction done current year and was latest, beehive combs value is changed and old value is stored to new nest reduction
            var      currentYear   = DateTime.Now.Year;
            DateTime reductionDate = nestReductionCreateDTO.Date ?? DateTime.Today;

            if (reductionDate.Year == currentYear)
            {
                await _context.Entry(beefamily).Collection(b => b.NestReductions).LoadAsync();

                var lastNestReduction = beefamily.NestReductions.OrderByDescending(ns => ns.Date).FirstOrDefault();
                if (lastNestReduction == null ||
                    DateTime.Compare(reductionDate, lastNestReduction.Date) > 0)
                {
                    if (beehive.Type == BeehiveTypes.Dadano)
                    {
                        nestReduction.CombsBefore     = beehive.NestCombs;
                        beehive.NestCombs             = nestReductionCreateDTO.StayedCombs;
                        _context.Entry(beehive).State = EntityState.Modified;
                    }
                }
                else
                {
                    return(BadRequest("Invalid date"));
                }
            }

            _context.NestReductions.Add(nestReduction);
            await _context.SaveChangesAsync();

            var nestReductionReadDTO = _mapper.Map <NestReductionReadDTO>(nestReduction);

            return(CreatedAtAction(nameof(GetNestReduction), new { id = nestReduction.Id }, nestReductionReadDTO));
        }
Ejemplo n.º 18
0
        public async Task <ActionResult <BeehiveComponentReadDTO> > CreateBeehiveComponent(BeehiveComponentCreateDTO beehiveComponentCreateDTO)
        {
            var beehive = await _context.Beehives.FindAsync(beehiveComponentCreateDTO.BeehiveId);

            if (beehive == null)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, beehive.FarmId);

            if (farmWorker == null || farmWorker.Permissions[6] != '1')
            {
                return(Forbid());
            }

            var component = _mapper.Map <BeehiveComponent>(beehiveComponentCreateDTO);

            if (!IsComponentTypeValid(component))
            {
                return(BadRequest("Invalid component Type"));
            }
            if (!IsComponentDataValid(component))
            {
                return(BadRequest("Invalid data"));
            }

            if (beehive.Type == BeehiveTypes.Daugiaaukštis)
            {
                var beehiveSupers = await _context.BeehiveComponents.Where(s => s.BeehiveId == beehive.Id && s.Type == ComponentTypes.Aukštas)
                                    .OrderBy(s => s.Position).ToArrayAsync();

                var lastBeehiveSuper = beehiveSupers.LastOrDefault();

                //If component is super
                if (component.Type == ComponentTypes.Aukštas)
                {
                    //Checks if new super position is correct
                    if (lastBeehiveSuper != null)
                    {
                        if (lastBeehiveSuper.Position + 1 >= component.Position)
                        {
                            //Converts from int? to int, "if" statement checks if position have valid value, but this statement is redundant if above
                            //validation working correctly
                            int startPosition = (component.Position ?? 0) - 1;
                            if (startPosition < 0)
                            {
                                return(BadRequest());
                            }
                            //If new super is inserted in beehive, then all above supers positions increased
                            for (int i = startPosition; i < beehiveSupers.Length; i++)
                            {
                                beehiveSupers[i].Position++;
                                _context.Entry(beehiveSupers[i]).State = EntityState.Modified;
                            }

                            //If inserted super is lower than queen excluder, then quen excluder position increased by 1
                            var beehiveQueenExcluder = await _context.BeehiveComponents.FirstOrDefaultAsync(s => s.BeehiveId == beehive.Id &&
                                                                                                            s.Type == ComponentTypes.SkiriamojiTvorelė);

                            if (beehiveQueenExcluder != null && component.Position <= beehiveQueenExcluder.Position)
                            {
                                beehiveQueenExcluder.Position++;
                                _context.Entry(beehiveQueenExcluder).State = EntityState.Modified;
                            }

                            //If inserted super is lower than bee decreaser, then bee decreaser position increased by 1
                            var beehiveBeeDecreaser = await _context.BeehiveComponents.FirstOrDefaultAsync(s => s.BeehiveId == beehive.Id &&
                                                                                                           s.Type == ComponentTypes.Išleistuvas);

                            if (beehiveBeeDecreaser != null && component.Position <= beehiveBeeDecreaser.Position)
                            {
                                beehiveBeeDecreaser.Position++;
                                _context.Entry(beehiveBeeDecreaser).State = EntityState.Modified;
                            }
                        }
                        else
                        {
                            return(BadRequest("Incorrect Super position"));
                        }
                    }
                    else if (component.Position != 1)
                    {
                        return(BadRequest("Incorrect Super position"));
                    }
                }

                if (component.Type == ComponentTypes.SkiriamojiTvorelė)
                {
                    var beehiveQueenExcluder = await _context.BeehiveComponents.FirstOrDefaultAsync(s => s.BeehiveId == beehive.Id &&
                                                                                                    s.Type == ComponentTypes.SkiriamojiTvorelė);

                    if (beehiveQueenExcluder != null)
                    {
                        return(BadRequest("Queen Excluder already exist"));
                    }
                    if (lastBeehiveSuper == null || component.Position >= lastBeehiveSuper.Position)
                    {
                        return(BadRequest("Incorrect Queen Excluder position"));
                    }
                }

                if (component.Type == ComponentTypes.DugnoSklendė)
                {
                    var bottomGate = await _context.BeehiveComponents.FirstOrDefaultAsync(s => s.BeehiveId == beehive.Id &&
                                                                                          s.Type == ComponentTypes.DugnoSklendė);

                    if (bottomGate != null)
                    {
                        return(BadRequest("Bottom Gate already exist"));
                    }
                }

                if (component.Type == ComponentTypes.Išleistuvas)
                {
                    var beehiveBeeDecreaser = await _context.BeehiveComponents.FirstOrDefaultAsync(s => s.BeehiveId == beehive.Id &&
                                                                                                   s.Type == ComponentTypes.Išleistuvas);

                    if (beehiveBeeDecreaser != null)
                    {
                        return(BadRequest("Bee Decreaser already exist"));
                    }
                    if (lastBeehiveSuper == null || component.Position >= lastBeehiveSuper.Position)
                    {
                        return(BadRequest("Incorrect Bee Decreaser position"));
                    }
                }
            }

            if (beehive.Type == BeehiveTypes.Dadano)
            {
                double honeycombsSupersCount = await _context.BeehiveComponents.Where(s => s.BeehiveId == beehive.Id && s.Type == ComponentTypes.Meduvė)
                                               .CountAsync();

                double miniHoneycombsSupersCount = await _context.BeehiveComponents.Where(s => s.BeehiveId == beehive.Id && s.Type == ComponentTypes.Pusmeduvė)
                                                   .CountAsync();

                double supersSpaceLeft = (beehive.MaxHoneyCombsSupers ?? 0) - honeycombsSupersCount - miniHoneycombsSupersCount / 2;
                if (component.Type == ComponentTypes.Meduvė && supersSpaceLeft < 1)
                {
                    return(BadRequest("There is no space for \"Meduve\""));
                }
                if (component.Type == ComponentTypes.Pusmeduvė && supersSpaceLeft < 0.5)
                {
                    return(BadRequest("There is no space for \"Pusmeduve\""));
                }
            }

            _context.BeehiveComponents.Add(component);
            await _context.SaveChangesAsync();

            var componentReadDTO = _mapper.Map <BeehiveComponentReadDTO>(component);

            return(CreatedAtAction(nameof(GetBeehiveComponent), new { id = component.Id }, componentReadDTO));
        }
Ejemplo n.º 19
0
        public async Task <ActionResult <NestExpansionReadDTO> > CreateNestExpansion(NestExpansionCreateDTO nestExpansionCreateDTO)
        {
            var beefamily = await _context.BeeFamilies.FindAsync(nestExpansionCreateDTO.BeefamilyId);

            if (beefamily == null || beefamily.State == BeeFamilyStates.Išmirusi || beefamily.State == BeeFamilyStates.SujungtaSuKitaŠeima)
            {
                return(BadRequest());
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, beefamily.FarmId);

            if (farmWorker == null || farmWorker.Permissions[12] != '1')
            {
                return(Forbid());
            }

            if (!IsFrameTypeValid(nestExpansionCreateDTO.FrameType ?? 0))
            {
                return(BadRequest());
            }

            if (nestExpansionCreateDTO.FrameType == FrameType.NestFrame)
            {
                DateTime expansionDate = nestExpansionCreateDTO.Date ?? DateTime.Today;
                if (IsDateRecent(expansionDate))
                {
                    var beehiveBeefamily = await _context.BeehiveBeeFamilies.FirstOrDefaultAsync(bb => bb.BeeFamilyId == beefamily.Id &&
                                                                                                 bb.DepartDate == null);

                    if (beehiveBeefamily == null)
                    {
                        return(BadRequest());
                    }
                    var beehive = await _context.Beehives.FindAsync(beehiveBeefamily.BeehiveId);

                    if (beehive == null)
                    {
                        return(BadRequest());
                    }
                    if (beehive.Type == BeehiveTypes.Dadano)
                    {
                        int newNestCombs = (beehive.NestCombs ?? 0) + (nestExpansionCreateDTO.CombSheets ?? 0) + (nestExpansionCreateDTO.Combs ?? 0);
                        if (newNestCombs > beehive.MaxNestCombs)
                        {
                            beehive.NestCombs = beehive.MaxNestCombs;
                        }
                        else
                        {
                            beehive.NestCombs = newNestCombs;
                        }
                        _context.Entry(beehive).State = EntityState.Modified;
                        await _context.SaveChangesAsync();
                    }
                }
            }

            var nestExpansion = _mapper.Map <NestExpansion>(nestExpansionCreateDTO);

            _context.NestExpansions.Add(nestExpansion);
            await _context.SaveChangesAsync();

            var nestExpansionReadDTO = _mapper.Map <NestExpansionReadDTO>(nestExpansion);

            return(CreatedAtAction("GetNestExpansion", "NestExpansions", new { id = nestExpansion.Id }, nestExpansionReadDTO));
        }
Ejemplo n.º 20
0
        public async Task <ActionResult <BeeFamilyReadDTO> > CreateBeeFamily(BeeFamilyCreateDTO beeFamilyCreateDTO)
        {
            var farm = await _context.Farms.FindAsync(beeFamilyCreateDTO.FarmId);

            if (farm == null)
            {
                return(BadRequest("nera farmos"));
            }

            var currentUserId = long.Parse(User.Identity.Name);
            var farmWorker    = await _context.FarmWorkers.FindAsync(currentUserId, farm.Id);

            if (farmWorker == null || farmWorker.Permissions[0] != '1')
            {
                return(Forbid());
            }

            if (beeFamilyCreateDTO.Origin == null || !IsOriginCorrect(beeFamilyCreateDTO.Origin ?? 0))
            {
                return(BadRequest("Incorrect Origin"));
            }

            var beehive = await _context.Beehives.FindAsync(beeFamilyCreateDTO.BeehiveId);

            var apiary = await _context.Apiaries.FindAsync(beeFamilyCreateDTO.ApiaryId);

            if (beehive == null || apiary == null)
            {
                return(BadRequest("nera avilio arba bityno"));
            }

            if (beehive.Type == BeehiveTypes.Dadano && beeFamilyCreateDTO.NestCombs > beehive.MaxNestCombs)
            {
                return(BadRequest("Nest combs count is higher than beehive maximum nest combs value"));
            }

            //Creates bee family
            var beeFamily = _mapper.Map <BeeFamily>(beeFamilyCreateDTO);

            beeFamily.IsNucleus = false;
            beeFamily.State     = BeeFamilyStates.Gyvena;
            _context.BeeFamilies.Add(beeFamily);
            await _context.SaveChangesAsync();

            //Creates apiary bee family
            ApiaryBeeFamily apiaryBeeFamily = new ApiaryBeeFamily()
            {
                ApiaryId    = apiary.Id,
                BeeFamilyId = beeFamily.Id,
                ArriveDate  = beeFamilyCreateDTO.ArriveDate
            };

            _context.ApiaryBeeFamilies.Add(apiaryBeeFamily);
            await _context.SaveChangesAsync();

            //Creates beehive bee family
            BeehiveBeeFamily beehiveBeeFamily = new BeehiveBeeFamily()
            {
                BeehiveId   = beehive.Id,
                BeeFamilyId = beeFamily.Id,
                ArriveDate  = beeFamilyCreateDTO.ArriveDate
            };

            _context.BeehiveBeeFamilies.Add(beehiveBeeFamily);
            await _context.SaveChangesAsync();

            //Updates beehive
            beehive.IsEmpty = false;
            if (beehive.Type == BeehiveTypes.Dadano)
            {
                beehive.NestCombs = beeFamilyCreateDTO.NestCombs;
            }
            _context.Entry(beehive).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            //Adds supers
            if (beehive.Type == BeehiveTypes.Daugiaaukštis)
            {
                for (int i = 0; i < beeFamilyCreateDTO.SupersCount; i++)
                {
                    BeehiveComponent super = new BeehiveComponent()
                    {
                        Type             = ComponentTypes.Aukštas,
                        Position         = i + 1,
                        InstallationDate = beeFamilyCreateDTO.ArriveDate,
                        BeehiveId        = beehive.Id
                    };
                    _context.BeehiveComponents.Add(super);
                }
                await _context.SaveChangesAsync();
            }

            var beeFamilyReadDTO = _mapper.Map <BeeFamilyReadDTO>(beeFamily);

            return(CreatedAtAction(nameof(GetBeeFamily), new { id = beeFamily.Id }, beeFamilyReadDTO));
        }