Esempio n. 1
0
        public async Task <IHttpActionResult> PostCountry(CountryCreateEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            model.Id = Guid.NewGuid();
            var country = new Country
            {
                Id       = model.Id,
                Name     = model.Name,
                PhotoUrl = model.PhotoUrl
            };

            db.Countries.Add(country);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (CountryExists(model.Id))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("DefaultApi", new { id = model.Id }, model));
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> PutCountry(Guid id, CountryCreateEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

            var country = new Country {
                Id = id
            };

            db.Countries.Attach(country);
            db.Entry(country).CurrentValues.SetValues(model);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CountryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }