Example #1
0
        public IHttpActionResult Get()
        {
            try
            {
                using (var context = new BeeAppContext())
                {
                    var apiaries = context.Beekeepers
                                   .Where(x => x.ApplicationUserId == _applicationUserId)
                                   .Include(x => x.Apiaries)
                                   .SelectMany(x => x.Apiaries)
                                   .Select(x => new
                    {
                        x.Id,
                        x.Name,
                        x.Place,
                        x.Longtitude,
                        x.Latitude
                    })
                                   .ToArray();

                    // Return
                    return(Ok(apiaries));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Example #2
0
        public async Task <IHttpActionResult> Get()
        {
            var applicationUserId = User.Identity.GetUserId();

            try
            {
                using (var context = new BeeAppContext())
                {
                    var beekeeper = await context.Beekeepers
                                    .Where(x => x.ApplicationUserId == applicationUserId)
                                    .Select(x => new
                    {
                        x.Id,
                        x.FirstName,
                        x.LastName,
                        x.Email,
                        x.PhoneNumber,
                        x.Number
                    })
                                    .ToArrayAsync();

                    // Return
                    return(Ok(beekeeper));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
Example #3
0
        public async Task <IHttpActionResult> UpdateInfo(BeekeeperUpdateInfo beekeeperUpdateInfo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Get current User Id
            var applicationUserId = User.Identity.GetUserId();

            using (var context = new BeeAppContext())
            {
                var beekeeper = await context.Beekeepers
                                .FirstOrDefaultAsync(x => x.ApplicationUserId == applicationUserId);

                if (beekeeper != null)
                {
                    beekeeper.FirstName   = beekeeperUpdateInfo.FirstName ?? beekeeper.FirstName;
                    beekeeper.LastName    = beekeeperUpdateInfo.LastName ?? beekeeper.LastName;
                    beekeeper.PhoneNumber = beekeeperUpdateInfo.Phone ?? beekeeper.PhoneNumber;
                    beekeeper.Number      = beekeeperUpdateInfo.Number ?? beekeeper.Number;

                    // Save
                    context.SaveChanges();

                    // Return
                    return(StatusCode(HttpStatusCode.NoContent));
                }
            }

            // Return
            return(Ok());
        }
Example #4
0
        public async Task <IHttpActionResult> Delete(int apiaryId)
        {
            try
            {
                using (var context = new BeeAppContext())
                {
                    // Get the apiary
                    var apiary = await context.Beekeepers
                                 .Where(x => x.ApplicationUserId == _applicationUserId)
                                 .Include(x => x.Apiaries)
                                 .SelectMany(x => x.Apiaries)
                                 .FirstOrDefaultAsync(x => x.Id == apiaryId);

                    if (apiary != null)
                    {
                        // Remove
                        context.Apiaries.Remove(apiary);

                        // Save
                        context.SaveChanges();

                        // Return
                        return(StatusCode(HttpStatusCode.NoContent));
                    }

                    // Return
                    return(BadRequest("Apiary could not be found"));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
Example #5
0
        public IHttpActionResult Get()
        {
            try
            {
                using (var context = new BeeAppContext())
                {
                    var works = context.Beekeepers
                                .Where(x => x.ApplicationUserId == _applicationUserId)
                                .Include(x => x.Works)
                                .SelectMany(x => x.Works)
                                .Select(x => new
                    {
                        x.Id,
                        x.Name,
                        x.Date,
                        x.Note,
                        x.IsCompleted
                    })
                                .ToArray();

                    // Return
                    return(Ok(works));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Example #6
0
        public async Task <IHttpActionResult> Delete(int workId)
        {
            try
            {
                using (var context = new BeeAppContext())
                {
                    var work = await context.Beekeepers
                               .Where(x => x.ApplicationUserId == _applicationUserId)
                               .Include(x => x.Works)
                               .SelectMany(x => x.Works)
                               .Where(x => x.Id == workId)
                               .FirstOrDefaultAsync();

                    if (work != null)
                    {
                        // Remove
                        context.Works.Remove(work);

                        // Save
                        context.SaveChanges();

                        // Return
                        return(StatusCode(HttpStatusCode.NoContent));
                    }

                    // Return
                    return(BadRequest("Work could not be found"));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
Example #7
0
        public async Task <IHttpActionResult> Post(BeekeeperModel beekeeperModel)
        {
            var user = new ApplicationUser
            {
                UserName    = beekeeperModel.Email,
                Email       = beekeeperModel.Email,
                FirstName   = beekeeperModel.FirstName,
                LastName    = beekeeperModel.LastName,
                PhoneNumber = beekeeperModel.Phone,
                Number      = beekeeperModel.Number
            };

            try
            {
                await _ensurer.EnsureEmailIsUnique(ModelState, UserManager, beekeeperModel.Email);

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var addUserResult = await UserManager.CreateAsync(user, beekeeperModel.Password);

                if (!addUserResult.Succeeded)
                {
                    return(GetErrorResult(addUserResult));
                }

                using (var context = new BeeAppContext())
                {
                    context.Beekeepers.Add(new Beekeeper
                    {
                        FirstName         = beekeeperModel.FirstName,
                        LastName          = beekeeperModel.LastName,
                        Email             = beekeeperModel.Email,
                        PhoneNumber       = beekeeperModel.Phone,
                        Number            = beekeeperModel.Number,
                        ApplicationUserId = user.Id
                    });

                    // Save
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            // Return
            return(Ok(new
            {
                user.Id,
                user.Email
            }));
        }
        public async Task <IHttpActionResult> Put(int inspectionId, InspectionModel inspectionModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Inspection Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    var inspection = await context.Beekeepers
                                     .Where(x => x.ApplicationUserId == _applicationUserId)
                                     .Include(x => x.Apiaries)
                                     .SelectMany(x => x.Apiaries)
                                     .Include(x => x.Hives)
                                     .SelectMany(x => x.Hives)
                                     .Include(x => x.Inspections)
                                     .SelectMany(x => x.Inspections)
                                     .Where(x => x.Id == inspectionId)
                                     .FirstOrDefaultAsync();

                    if (inspection != null)
                    {
                        inspection.Name              = inspectionModel.Name;
                        inspection.Date              = inspectionModel.Date;
                        inspection.Strength          = inspectionModel.Strength;
                        inspection.Temper            = inspectionModel.Temper;
                        inspection.Disease           = inspectionModel.Disease;
                        inspection.FramesBees        = inspectionModel.FramesBees;
                        inspection.FramesHoney       = inspectionModel.FramesHoney;
                        inspection.FramesHoneySupers = inspectionModel.FramesHoneySupers;
                        inspection.Drones            = inspectionModel.Drones;
                        inspection.DroneCells        = inspectionModel.DroneCells;

                        // Save
                        context.SaveChanges();

                        // Return
                        return(StatusCode(HttpStatusCode.NoContent));
                    }

                    // Return
                    return(BadRequest("Inspection could not be found"));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IHttpActionResult> Put(int feedingId, FeedingModel feedingModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Feeding Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    var feeding = await context.Beekeepers
                                  .Where(x => x.ApplicationUserId == _applicationUserId)
                                  .Include(x => x.Apiaries)
                                  .SelectMany(x => x.Apiaries)
                                  .Include(x => x.Hives)
                                  .SelectMany(x => x.Hives)
                                  .Include(x => x.Feedings)
                                  .SelectMany(x => x.Feedings)
                                  .Where(x => x.Id == feedingId)
                                  .FirstOrDefaultAsync();

                    if (feeding != null)
                    {
                        feeding.Name     = feedingModel.Name;
                        feeding.Date     = feedingModel.Date;
                        feeding.Product  = feedingModel.Product;
                        feeding.Quantity = feedingModel.Quantity;
                        feeding.Unit     = feedingModel.Unit;
                        feeding.Note     = feedingModel.Note;

                        // Save
                        context.SaveChanges();

                        // Return
                        return(StatusCode(HttpStatusCode.NoContent));
                    }

                    // Return
                    return(BadRequest("Feeding could not be found"));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
Example #10
0
        public async Task <IHttpActionResult> Put(int queenId, QueenModel queenModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Queen Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    var queen = await context.Beekeepers
                                .Where(x => x.ApplicationUserId == _applicationUserId)
                                .Include(x => x.Apiaries)
                                .SelectMany(x => x.Apiaries)
                                .Include(x => x.Hives)
                                .SelectMany(x => x.Hives)
                                .Include(x => x.Queens)
                                .SelectMany(x => x.Queens)
                                .Where(x => x.Id == queenId)
                                .FirstOrDefaultAsync();

                    if (queen != null)
                    {
                        queen.Name   = queenModel.Name;
                        queen.Date   = queenModel.Date;
                        queen.Breed  = queenModel.Breed;
                        queen.Colour = queenModel.Colour;
                        queen.State  = queenModel.State;
                        queen.Status = queenModel.Status;

                        // Save
                        context.SaveChanges();

                        // Return
                        return(StatusCode(HttpStatusCode.NoContent));
                    }

                    // Return
                    return(BadRequest("Queen could not be found"));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IHttpActionResult> Put(int treatmentId, TreatmentModel treatmentModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Treatment Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    var treatment = await context.Beekeepers
                                    .Where(x => x.ApplicationUserId == _applicationUserId)
                                    .Include(x => x.Apiaries)
                                    .SelectMany(x => x.Apiaries)
                                    .Include(x => x.Hives)
                                    .SelectMany(x => x.Hives)
                                    .Include(x => x.Treatments)
                                    .SelectMany(x => x.Treatments)
                                    .Where(x => x.Id == treatmentId)
                                    .FirstOrDefaultAsync();

                    if (treatment != null)
                    {
                        treatment.Name     = treatmentModel.Name;
                        treatment.Date     = treatmentModel.Date;
                        treatment.Product  = treatmentModel.Product;
                        treatment.Quantity = treatmentModel.Quantity;
                        treatment.Unit     = treatmentModel.Unit;
                        treatment.Note     = treatmentModel.Note;

                        // Save
                        context.SaveChanges();

                        // Return
                        return(StatusCode(HttpStatusCode.NoContent));
                    }

                    // Return
                    return(BadRequest("Treatment could not be found"));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IHttpActionResult> Put(int hiveId, HiveModel hiveModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Hive Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    var hive = await context.Beekeepers
                               .Where(x => x.ApplicationUserId == _applicationUserId)
                               .Include(x => x.Apiaries)
                               .SelectMany(x => x.Apiaries)
                               .Include(x => x.Hives)
                               .SelectMany(x => x.Hives)
                               .Where(x => x.Id == hiveId)
                               .FirstOrDefaultAsync();

                    if (hive != null)
                    {
                        hive.Name         = hiveModel.Name;
                        hive.Date         = hiveModel.Date;
                        hive.Status       = hiveModel.Status;
                        hive.Type         = hiveModel.Type;
                        hive.Note         = hiveModel.Note;
                        hive.Family       = hiveModel.Family;
                        hive.FamilyOrigin = hiveModel.FamilyOrigin;

                        // Save
                        context.SaveChanges();

                        // Return
                        return(StatusCode(HttpStatusCode.NoContent));
                    }

                    // Return
                    return(BadRequest("Hive could not be found"));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
        /// <summary>
        /// Ensures the creating in own apiary.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="apiaryId">The apiary identifier.</param>
        /// <returns></returns>
        private bool EnsureCreatingInOwnApiary(BeeAppContext context, int apiaryId)
        {
            var result = false;

            var beekeeperApiaries = context.Beekeepers
                                    .Where(x => x.ApplicationUserId == _applicationUserId)
                                    .Include(x => x.Apiaries)
                                    .SelectMany(x => x.Apiaries)
                                    .Where(x => x.Id == apiaryId);

            if (beekeeperApiaries.Any())
            {
                result = true;
            }

            // Return
            return(result);
        }
        public async Task <IHttpActionResult> Get(int apiaryId, int hiveId, int inspectionId)
        {
            try
            {
                using (var context = new BeeAppContext())
                {
                    var inspection = await context.Beekeepers
                                     .Where(x => x.ApplicationUserId == _applicationUserId)
                                     .Include(x => x.Apiaries)
                                     .SelectMany(x => x.Apiaries)
                                     .Where(x => x.Id == apiaryId)
                                     .Include(x => x.Hives)
                                     .SelectMany(x => x.Hives)
                                     .Where(x => x.Id == hiveId)
                                     .Include(x => x.Inspections)
                                     .SelectMany(x => x.Inspections)
                                     .Where(x => x.Id == inspectionId)
                                     .Select(x => new
                    {
                        x.Id,
                        x.Date,
                        x.Name,
                        x.Strength,
                        x.Temper,
                        x.Disease,
                        x.FramesBees,
                        x.FramesHoney,
                        x.FramesHoneySupers,
                        x.Drones,
                        x.DroneCells
                    })
                                     .FirstOrDefaultAsync();

                    // Return
                    return(Ok(inspection));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IHttpActionResult> Post(int apiaryId, int hiveId, InspectionModel inspectionModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Inspection Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    if (await _ensurer.EnsureHiveBelongsToApiary(context, apiaryId, hiveId, _applicationUserId))
                    {
                        context.Inspections.Add(new Inspection
                        {
                            Date              = inspectionModel.Date,
                            Name              = inspectionModel.Name,
                            Strength          = inspectionModel.Strength,
                            Temper            = inspectionModel.Temper,
                            Disease           = inspectionModel.Disease,
                            FramesBees        = inspectionModel.FramesBees,
                            FramesHoney       = inspectionModel.FramesHoney,
                            FramesHoneySupers = inspectionModel.FramesHoneySupers,
                            Drones            = inspectionModel.Drones,
                            DroneCells        = inspectionModel.DroneCells,
                            HiveId            = hiveId
                        });

                        // Save
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            // Return
            return(Ok(inspectionModel));
        }
Example #16
0
        public async Task <IHttpActionResult> Put(int workId, WorkModel workModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Work Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    var work = await context.Beekeepers
                               .Where(x => x.ApplicationUserId == _applicationUserId)
                               .Include(x => x.Works)
                               .SelectMany(x => x.Works)
                               .FirstOrDefaultAsync(x => x.Id == workId);

                    if (work != null)
                    {
                        work.Name        = workModel.Name;
                        work.Date        = workModel.Date;
                        work.Note        = workModel.Note;
                        work.IsCompleted = workModel.IsCompleted;

                        // Save
                        context.SaveChanges();

                        // Return
                        return(StatusCode(HttpStatusCode.NoContent));
                    }

                    // Return
                    return(BadRequest("Work could not be found"));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
Example #17
0
        public async Task <IHttpActionResult> Put(int apiaryId, ApiaryModel apiaryModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Apiary Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    var apiary = await context.Beekeepers
                                 .Where(x => x.ApplicationUserId == _applicationUserId)
                                 .Include(x => x.Apiaries)
                                 .SelectMany(x => x.Apiaries)
                                 .FirstOrDefaultAsync(x => x.Id == apiaryId);

                    if (apiary != null)
                    {
                        apiary.Name       = apiaryModel.Name;
                        apiary.Place      = apiaryModel.Place;
                        apiary.Longtitude = apiaryModel.Longtitude;
                        apiary.Latitude   = apiaryModel.Latitude;

                        // Save
                        context.SaveChanges();

                        // Return
                        return(StatusCode(HttpStatusCode.NoContent));
                    }

                    // Return
                    return(BadRequest("Apiary could not be found"));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
Example #18
0
        public async Task <IHttpActionResult> Post(WorkModel workModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Work Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    var beekeeper = await context.Beekeepers
                                    .FirstOrDefaultAsync(x => x.ApplicationUserId == _applicationUserId);

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

                    // Add work to a beekeeper
                    beekeeper.Works.Add(new Work
                    {
                        Name        = workModel.Name,
                        Date        = workModel.Date,
                        Note        = workModel.Note,
                        IsCompleted = workModel.IsCompleted
                    });

                    // Save
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            // Return
            return(Ok(workModel));
        }
        public IHttpActionResult Post(int apiaryId, HiveModel hiveModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Hive Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    if (!EnsureCreatingInOwnApiary(context, apiaryId))
                    {
                        return(BadRequest("The apiary could not be found."));
                    }

                    context.Hives.Add(new Hive
                    {
                        Name         = hiveModel.Name,
                        Date         = hiveModel.Date,
                        Status       = hiveModel.Status,
                        Type         = hiveModel.Type,
                        Note         = hiveModel.Note,
                        Family       = hiveModel.Family,
                        FamilyOrigin = hiveModel.FamilyOrigin,
                        ApiaryId     = apiaryId
                    });

                    // Save
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            // Return
            return(Ok(hiveModel));
        }
Example #20
0
        public async Task <IHttpActionResult> Post(ApiaryModel apiaryModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Apiary Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    var beekeeper = await context.Beekeepers
                                    .FirstOrDefaultAsync(x => x.ApplicationUserId == _applicationUserId);

                    if (beekeeper == null)
                    {
                        return(BadRequest("The application user could not be found."));
                    }

                    beekeeper.Apiaries.Add(new Apiary
                    {
                        Name       = apiaryModel.Name,
                        Place      = apiaryModel.Place,
                        Longtitude = apiaryModel.Longtitude,
                        Latitude   = apiaryModel.Latitude
                    });

                    // Save
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            // Return
            return(Ok(apiaryModel));
        }
Example #21
0
        public async Task <IHttpActionResult> Get(int apiaryId, int hiveId, int queenId)
        {
            try
            {
                using (var context = new BeeAppContext())
                {
                    var queen = await context.Beekeepers
                                .Where(x => x.ApplicationUserId == _applicationUserId)
                                .Include(x => x.Apiaries)
                                .SelectMany(x => x.Apiaries)
                                .Where(x => x.Id == apiaryId)
                                .Include(x => x.Hives)
                                .SelectMany(x => x.Hives)
                                .Where(x => x.Id == hiveId)
                                .Include(x => x.Queens)
                                .SelectMany(x => x.Queens)
                                .Where(x => x.Id == queenId)
                                .Select(x => new
                    {
                        x.Id,
                        x.Name,
                        x.Date,
                        x.Breed,
                        x.Colour,
                        x.State,
                        x.Status
                    })
                                .FirstOrDefaultAsync();

                    // Return
                    return(Ok(queen));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IHttpActionResult> Get(int apiaryId, int hiveId, int feedingId)
        {
            try
            {
                using (var context = new BeeAppContext())
                {
                    var feeding = await context.Beekeepers
                                  .Where(x => x.ApplicationUserId == _applicationUserId)
                                  .Include(x => x.Apiaries)
                                  .SelectMany(x => x.Apiaries)
                                  .Where(x => x.Id == apiaryId)
                                  .Include(x => x.Hives)
                                  .SelectMany(x => x.Hives)
                                  .Where(x => x.Id == hiveId)
                                  .Include(x => x.Feedings)
                                  .SelectMany(x => x.Feedings)
                                  .Where(x => x.Id == feedingId)
                                  .Select(x => new
                    {
                        x.Id,
                        x.Name,
                        x.Date,
                        x.Product,
                        x.Quantity,
                        x.Unit,
                        x.Note
                    })
                                  .FirstOrDefaultAsync();

                    // Return
                    return(Ok(feeding));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
        public IHttpActionResult Get(int apiaryId)
        {
            try
            {
                using (var context = new BeeAppContext())
                {
                    var feedings = context.Beekeepers
                                   .Where(x => x.ApplicationUserId == _applicationUserId)
                                   .Include(x => x.Apiaries)
                                   .SelectMany(x => x.Apiaries)
                                   .Where(x => x.Id == apiaryId)
                                   .Include(x => x.Hives)
                                   .SelectMany(x => x.Hives)
                                   .Include(x => x.Feedings)
                                   .SelectMany(x => x.Feedings)
                                   .Select(x => new
                    {
                        x.Id,
                        x.Name,
                        x.Date,
                        x.Product,
                        x.Quantity,
                        x.Unit,
                        x.Note,
                        x.HiveId
                    })
                                   .ToArray();

                    // Return
                    return(Ok(feedings));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
Example #24
0
        /// <summary>
        /// Ensures the hive belongs to apiary.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="apiaryId">The apiary identifier.</param>
        /// <param name="hiveId">The hive identifier.</param>
        /// <param name="userId">The user identifier.</param>
        /// <returns></returns>
        public async Task <bool> EnsureHiveBelongsToApiary(BeeAppContext context, int apiaryId, int hiveId, string userId)
        {
            var result = false;

            // Search for specific apiary and hive
            var hive = await context.Beekeepers
                       .Where(x => x.ApplicationUserId == userId)
                       .Include(x => x.Apiaries)
                       .SelectMany(x => x.Apiaries)
                       .Where(x => x.Id == apiaryId)
                       .Include(x => x.Hives)
                       .SelectMany(x => x.Hives)
                       .Where(x => x.Id == hiveId)
                       .FirstOrDefaultAsync();

            if (hive != null)
            {
                result = true;
            }

            // Return
            return(result);
        }
        public async Task <IHttpActionResult> Post(int apiaryId, int hiveId, FeedingModel feedingModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Feeding Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    if (await _ensurer.EnsureHiveBelongsToApiary(context, apiaryId, hiveId, _applicationUserId))
                    {
                        context.Feedings.Add(new Feeding
                        {
                            Name     = feedingModel.Name,
                            Date     = feedingModel.Date,
                            Product  = feedingModel.Product,
                            Quantity = feedingModel.Quantity,
                            Unit     = feedingModel.Unit,
                            Note     = feedingModel.Note,
                            HiveId   = hiveId
                        });

                        // Save
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            // Return
            return(Ok(feedingModel));
        }
Example #26
0
        public async Task <IHttpActionResult> Post(int apiaryId, int hiveId, QueenModel queenModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Queen Model is not valid"));
            }

            try
            {
                using (var context = new BeeAppContext())
                {
                    if (await _ensurer.EnsureHiveBelongsToApiary(context, apiaryId, hiveId, _applicationUserId))
                    {
                        context.Queens.Add(new Queen
                        {
                            Name   = queenModel.Name,
                            Date   = queenModel.Date,
                            Breed  = queenModel.Breed,
                            Colour = queenModel.Colour,
                            State  = queenModel.State,
                            Status = queenModel.Status,
                            HiveId = hiveId
                        });

                        // Save
                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            // Return
            return(Ok(queenModel));
        }
        public IHttpActionResult Get(int apiaryId)
        {
            try
            {
                using (var context = new BeeAppContext())
                {
                    var hives = context.Beekeepers
                                .Where(x => x.ApplicationUserId == _applicationUserId)
                                .Include(x => x.Apiaries)
                                .SelectMany(x => x.Apiaries)
                                .Where(x => x.Id == apiaryId)
                                .Include(x => x.Hives)
                                .SelectMany(x => x.Hives)
                                .Select(x => new
                    {
                        x.Id,
                        x.Name,
                        x.Date,
                        x.Status,
                        x.Type,
                        x.Note,
                        x.Family,
                        x.FamilyOrigin
                    })
                                .ToArray();

                    // Return
                    return(Ok(hives));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }
        public IHttpActionResult Get(int hiveId)
        {
            try
            {
                using (var context = new BeeAppContext())
                {
                    var monitoring = context.Beekeepers
                                     .Where(x => x.ApplicationUserId == _applicationUserId)
                                     .Include(x => x.Apiaries)
                                     .SelectMany(x => x.Apiaries)
                                     .Include(x => x.Hives)
                                     .SelectMany(x => x.Hives)
                                     .Where(x => x.Id == hiveId)
                                     .Include(x => x.Monitoring)
                                     .SelectMany(x => x.Monitoring)
                                     .Select(x => new
                    {
                        x.Id,
                        x.Timestamp,
                        x.Temperature,
                        x.Humidity,
                        x.Longtitude,
                        x.Latitude
                    })
                                     .ToArray();

                    // Return
                    return(Ok(monitoring));
                }
            }
            catch (Exception ex)
            {
                // Return
                return(BadRequest(ex.Message));
            }
        }