public HttpResponseMessage GetById(int id) { try { if (id <= 0) { return(Request.CreateResponse(HttpStatusCode.BadRequest, $"Id {id} is not valid")); } var chatroomEntity = chatroomBusiness.GetById(id); if (chatroomEntity == null) { return(Request.CreateResponse(HttpStatusCode.NotFound, $"Chatroom with id {id} does not exist.")); } var chatroomModel = mapper.MapFromEntityToModel(chatroomEntity); return(Request.CreateResponse(HttpStatusCode.OK, chatroomModel)); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
public HttpResponseMessage Create([FromBody] MessageViewModel model) { try { if (model.ChatroomId <= 0) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid chatroom id.")); } if (chatroomBusiness.GetById(model.ChatroomId) == null) { return(Request.CreateResponse(HttpStatusCode.NotFound, $"Chatroom with id {model.ChatroomId} does not exist.")); } if (string.IsNullOrEmpty(model.UserSender)) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid user sender.")); } if (userBusiness.GetByNickName(model.UserSender) == null) { return(Request.CreateResponse(HttpStatusCode.NotFound, $"User sender {model.UserSender} does not exist.")); } model.Date = DateTime.Now; var messageEntity = mapper.MapFromModelToEntity(model); var messageId = messageBusiness.Create(messageEntity); if (messageId == 2) { return(Request.CreateResponse(HttpStatusCode.OK)); } messageEntity = messageBusiness.GetById(messageId); var messageModel = mapper.MapFromEntityToModel(messageEntity); return(Request.CreateResponse(HttpStatusCode.Created, messageModel)); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message)); } }