public async Task <ActionResult <IEnumerable <ApiaryBeeFamilyReadForApiaryDTO> > > GetAllApiaryBeeFamilies(long apiaryId) { var apiary = await _context.Apiaries.FindAsync(apiaryId); if (apiary == null) { return(BadRequest()); } var currentUserId = long.Parse(User.Identity.Name); var farmWorker = await _context.FarmWorkers.FindAsync(currentUserId, apiary.FarmId); if (farmWorker == null) { return(Forbid()); } var beeFamiliesList = await _context.ApiaryBeeFamilies.Where(ab => ab.ApiaryId == apiaryId && ab.DepartDate == null).ToListAsync(); foreach (ApiaryBeeFamily apiaryBeeFamily in beeFamiliesList) { await _context.Entry(apiaryBeeFamily).Reference(ab => ab.BeeFamily).LoadAsync(); } return(_mapper.Map <IEnumerable <ApiaryBeeFamilyReadForApiaryDTO> >(beeFamiliesList).ToList()); }
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)); }
public async Task <ActionResult <IEnumerable <FeedingReadDTO> > > GetBeefamilyFeedings(long beefamilyId) { var beefamily = await _context.BeeFamilies.FindAsync(beefamilyId); if (beefamily == null) { return(NotFound()); } var currentUserId = long.Parse(User.Identity.Name); var farmWorker = await _context.FarmWorkers.FindAsync(currentUserId, beefamily.FarmId); if (farmWorker == null) { return(Forbid()); } var feedings = await _context.Feedings.Where(f => f.BeeFamilyId == beefamilyId).ToListAsync(); foreach (Feeding feeding in feedings) { await _context.Entry(feeding).Reference(f => f.Food).LoadAsync(); } return(_mapper.Map <IEnumerable <FeedingReadDTO> >(feedings).ToList()); }
public async Task <IActionResult> PutManufacturer(long id, Manufacturer manufacturer) { if (id != manufacturer.Id) { return(BadRequest()); } _context.Entry(manufacturer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ManufacturerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <ApiaryReadDTO> > DeleteApiary(long id) { var apiary = await _context.Apiaries.FindAsync(id); if (apiary == null) { return(NotFound()); } var farm = await _context.Farms.FindAsync(apiary.FarmId); if (farm == null) { return(NotFound()); } 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()); } await _context.Entry(apiary).Collection(a => a.Harvests).LoadAsync(); _context.Apiaries.Remove(apiary); await _context.SaveChangesAsync(); return(_mapper.Map <ApiaryReadDTO>(apiary)); }
public async Task <ActionResult <IEnumerable <BeehiveBeeFamilyReadDTO> > > GetBeefamilyBeehives(long id) { var beefamily = await _context.BeeFamilies.FindAsync(id); if (beefamily == null) { return(NotFound()); } var currentUserId = long.Parse(User.Identity.Name); var farmWorker = await _context.FarmWorkers.FindAsync(currentUserId, beefamily.FarmId); if (farmWorker == null) { return(Forbid()); } var beefamilyBeehives = await _context.BeehiveBeeFamilies.Where(l => l.BeeFamilyId == id).ToListAsync(); foreach (BeehiveBeeFamily beefamilyBeehive in beefamilyBeehives) { await _context.Entry(beefamilyBeehive).Reference(ab => ab.Beehive).LoadAsync(); } return(_mapper.Map <IEnumerable <BeehiveBeeFamilyReadDTO> >(beefamilyBeehives).ToList()); }
public async Task <ActionResult <IEnumerable <QueensRaisingReadDTO> > > GetFarmQueensRaisings(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) { return(Forbid()); } var queensRaisingsList = await _context.QueensRaisings.Where(qr => qr.Mother.FarmId == farmId).ToListAsync(); foreach (var queensRaising in queensRaisingsList) { await _context.Entry(queensRaising).Reference(qr => qr.Mother).LoadAsync(); await _context.Entry(queensRaising).Reference(qr => qr.Beefamily).LoadAsync(); } return(_mapper.Map <IEnumerable <QueensRaisingReadDTO> >(queensRaisingsList).ToList()); }
public async Task <ActionResult <BeeFamilyQueenReadDTO> > GetBeeFamilyQueen(long id) { var beefamilyQueen = await _context.BeeFamilyQueens.FindAsync(id); if (beefamilyQueen == null) { return(NotFound()); } var beefamily = await _context.BeeFamilies.FindAsync(beefamilyQueen.BeeFamilyId); var currentUserId = long.Parse(User.Identity.Name); var farmWorker = await _context.FarmWorkers.FindAsync(currentUserId, beefamily.FarmId); if (farmWorker == null) { return(Forbid()); } await _context.Entry(beefamilyQueen).Reference(fq => fq.Queen).LoadAsync(); return(_mapper.Map <BeeFamilyQueenReadDTO>(beefamilyQueen)); }
public async Task <ActionResult <long> > ValidateInvitation(string code) { var invitation = await _context.Invitations.Where(l => l.Code.ToString() == code).FirstOrDefaultAsync(); if (invitation == null) { return(NotFound("Neteisingas kodas")); } if (invitation.ExpirationDate < DateTime.UtcNow) { return(Forbid()); } var currentUserId = long.Parse(User.Identity.Name); var farmWorker = await _context.FarmWorkers.FindAsync(currentUserId, invitation.FarmId); if (farmWorker != null) { return(Forbid()); } var user = await _context.Users.FindAsync(currentUserId); if (user.DefaultFarmId == null) { user.DefaultFarmId = invitation.FarmId; _context.Entry(user).State = EntityState.Modified; await _context.SaveChangesAsync(); } farmWorker = new FarmWorker { Role = WorkerRole.Assistant, FarmId = invitation.FarmId, UserId = currentUserId, Permissions = "000000000000000000000000000000" }; _context.FarmWorkers.Add(farmWorker); await _context.SaveChangesAsync(); return(invitation.FarmId); }
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)); }
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)); }
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)); }
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)); }