Exemple #1
0
        public ActionResult Post(LocationFormDto location)
        {
            if (ModelState.IsValid)
            {
                _repository.Add(location);
                ModelState.Clear();
                ViewBag.Message = "Success!";
                return(View());
            }

            return(View(location));
        }
Exemple #2
0
        public async Task <IActionResult> CreateLocation([FromBody] LocationFormDto location)
        {
            var locationType = await _locationRepo.GetLocationType(location.LocationTypeId);

            if (locationType == null)
            {
                ModelState.AddModelError("error", "Location Type not found");
            }


            if (string.IsNullOrEmpty(location.Name))
            {
                ModelState.AddModelError("error", "Location name is required");
            }

            if (string.IsNullOrEmpty(location.Address))
            {
                ModelState.AddModelError("error", "Location Address is required");
            }


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

            var newLocation = new Location
            {
                Name         = location.Name,
                Address      = location.Address,
                Phone        = location.Phone,
                LocationType = locationType
            };

            _locationRepo.Add(newLocation);

            if (await _locationRepo.Save())
            {
                var locationToReturn = _mapper.Map <LocationDetailDto>(newLocation);

                return(CreatedAtRoute("GetLocation", new { controller = "Locations", id = newLocation.Id }, locationToReturn));
            }


            return(BadRequest(new { error = "Unabble to Create new Location" }));
        }
Exemple #3
0
        public async Task <IActionResult> UpdateLocation(int id, [FromBody] LocationFormDto locationUpdate)
        {
            var location = await _locationRepo.GetLocation(id);

            if (location == null)
            {
                return(NotFound(new { error = new string[] { "Location Not Found" } }));
            }


            if (locationUpdate.LocationTypeId > 0)
            {
                var locationType = await _locationRepo.GetLocationType(locationUpdate.LocationTypeId);

                if (locationType == null)
                {
                    return(NotFound(new { error = new string[] { "Location Type Not Found" } }));
                }

                location.LocationType = locationType;
            }

            if (!string.IsNullOrEmpty(locationUpdate.Name))
            {
                location.Name = locationUpdate.Name;
            }

            if (!string.IsNullOrEmpty(locationUpdate.Address))
            {
                location.Address = locationUpdate.Address;
            }

            if (!string.IsNullOrEmpty(locationUpdate.Phone))
            {
                location.Phone = locationUpdate.Phone;
            }

            if (await _locationRepo.Save())
            {
                return(Ok(location));
            }

            return(BadRequest(new { error = new string[] { "Error Saving Location" } }));
        }
 public NewLocationInserted(LocationFormDto _location)
 {
     location = _location;
     Message  = Convert();
 }