public async Task <IActionResult> UpdateSuburb(int id, [FromBody] SuburbResource suburbResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var suburb = await repository.GetSuburb(id, includeRelated : false);

            if (suburb == null)
            {
                return(NotFound());
            }
            var existingSuburb = await repository.FindSuburb(suburbResource);

            if (existingSuburb != null)
            {
                ModelState.AddModelError("Message", "Suburb update error. Sorry, this suburb already exists!");
                return(BadRequest(ModelState));
            }
            mapper.Map <SuburbResource, Suburb>(suburbResource, suburb);
            suburb = await repository.PopulateSuburbWithRelatedFields(suburb, suburbResource);

            await unitOfWork.CompleteAsync();

            return(Ok(suburb));
        }
Example #2
0
 public async Task <Suburb> FindSuburb(SuburbResource suburbResource)
 {
     return(await context.Suburbs.SingleOrDefaultAsync(record =>
                                                       record.Postcode == suburbResource.Postcode &&
                                                       record.Name == suburbResource.Name &&
                                                       record.StateId == suburbResource.StateId));
 }
        public async Task <IActionResult> CreateSuburb([FromBody] SuburbResource suburbResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var suburb = await repository.FindSuburb(suburbResource);

            if (suburb != null)
            {
                ModelState.AddModelError("Message", "Suburb creation error. Sorry, this suburb already exists!");
                return(BadRequest(ModelState));
            }
            suburb = mapper.Map <SuburbResource, Suburb>(suburbResource);
            suburb = await repository.PopulateSuburbWithRelatedFields(suburb, suburbResource);

            repository.Add(suburb);
            await unitOfWork.CompleteAsync();

            return(Ok(suburb));
        }
Example #4
0
        public async Task <Suburb> PopulateSuburbWithRelatedFields(Suburb suburb, SuburbResource suburbResource)
        {
            suburb.State = await context.States.SingleOrDefaultAsync(state => state.Id == suburbResource.StateId);

            return(suburb);
        }