public async Task <IActionResult> AddAsync([FromBody] UserLocationAddModel mUserLocation) { if (!ModelState.IsValid) { return(HttpBadRequest(ModelStateError())); } var userLocationId = await _userLocationRepository.AddAsync(mUserLocation); return(CreatedAtRoute("GetByUserLocationIdAsync", new { controller = "UserLocations", userLocationId = userLocationId }, mUserLocation)); }
public async Task <int> AddAsync(UserLocationAddModel mUserLocation) { var location = _context.Locations.FirstOrDefault(c => c.LocationId == mUserLocation.LocationId); if (location == null) { throw new ExpectException("Could not find Location data which LocationId equal to " + mUserLocation.LocationId); } var user = _context.Users.FirstOrDefault(c => c.Id == mUserLocation.UserId); if (user == null) { throw new ExpectException("Could not find User data which UserId equal to '" + mUserLocation.UserId + "'"); } //LocationId and UserId must be unique var checkData = await _context.UserLocations .Where(c => c.LocationId == mUserLocation.LocationId && c.UserId == mUserLocation.UserId).ToListAsync(); if (checkData.Count > 0) { throw new ExpectException("There is already exist data which LocationId equal to " + mUserLocation.LocationId + " and UserId equal to '" + mUserLocation.UserId + "'"); } var model = new UserLocation { LocationId = mUserLocation.LocationId, UserId = mUserLocation.UserId }; _context.UserLocations.Add(model); await _context.SaveChangesAsync(); return(model.UserLocationId); }
public IActionResult SaveUserLocation([FromBody] UserLocationAddModel model) { if (!ModelState.IsValid) { throw new ValidationException("Input is invalid"); } var userId = model?.UserId?.Trim(); var locationId = model?.LocationId?.Trim(); var addressLine1 = model?.AddressLine1?.Trim(); var addressLine2 = model?.AddressLine2?.Trim(); var postcode = model?.Postcode?.Trim(); var suburb = model?.Suburb?.Trim(); var state = model?.State?.Trim(); var city = model?.City?.Trim(); var country = model?.Country?.Trim(); var latitude = model.Latitude; var longitude = model.Longitude; var isCurrent = model.IsCurrent; var notes = model?.Notes?.Trim(); var savedUserLocation = _repository.SaveLocationForUser( userId, locationId, addressLine1, addressLine2, postcode, suburb, state, city, country, latitude, longitude, isCurrent, notes); return(Ok(savedUserLocation)); }