// POST api/UserLocationDetailsDto public async Task <HttpResponseMessage> Post([FromBody] UserLocationDetailsDto fullUserLocationDetails) { try { var userModelAlreadyExists = m_userRepository.Get(fullUserLocationDetails.User.Id) != null; if (userModelAlreadyExists) { // we already have a user with this ID return(new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = "User with this ID already exists." }); } var assignedRoom = m_roomRepository.Get(fullUserLocationDetails.Room.Id); if (assignedRoom == null) { // we don't have a room with the data specified in the request return(new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = "No room with the data specified in the request seems to exist." }); } await m_userRepository.Add(new User { Id = fullUserLocationDetails.User.Id, Name = fullUserLocationDetails.User.FullName, PhotoUrl = fullUserLocationDetails.User.PhotoUrl, CoordX = fullUserLocationDetails.Coordinates.X, CoordY = fullUserLocationDetails.Coordinates.Y, SpecialCoord = fullUserLocationDetails.Coordinates.ComplexIdBased, Place = fullUserLocationDetails.Coordinates.Id ?? -1, Room = assignedRoom, RoomId = assignedRoom.Id }); return(new HttpResponseMessage(HttpStatusCode.OK)); } catch (Exception ex) { return(new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = $"Something went wrong when trying to add the user. ({ex.Message})" }); } }
// PUT api/UserLocationDetailsDto/5 public async Task <HttpResponseMessage> Put(int id, [FromBody] UserLocationDetailsDto fullUserLocationDetails) { try { var userToUpdate = m_userRepository.Get(fullUserLocationDetails.User.Id); if (userToUpdate == null) { // we already have a user with this ID return(new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = "User with this ID doesn't exist." }); } var assignedRoom = m_roomRepository.Get(fullUserLocationDetails.Room.Id); if (assignedRoom == null) { // we don't have a room with the data specified in the request return(new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = "No room with the data specified in the request seems to exist." }); } userToUpdate.Name = fullUserLocationDetails.User.FullName; userToUpdate.PhotoUrl = fullUserLocationDetails.User.PhotoUrl; userToUpdate.CoordX = fullUserLocationDetails.Coordinates.X; userToUpdate.CoordY = fullUserLocationDetails.Coordinates.Y; userToUpdate.SpecialCoord = fullUserLocationDetails.Coordinates.ComplexIdBased; userToUpdate.Place = fullUserLocationDetails.Coordinates.Id ?? -1; userToUpdate.Room = assignedRoom; userToUpdate.RoomId = assignedRoom.Id; await m_userRepository.Update(userToUpdate); return(new HttpResponseMessage(HttpStatusCode.OK)); } catch (Exception ex) { return(new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = $"Something went wrong when trying to add the user. ({ex.Message})" }); } }