Example #1
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 #2
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));
        }