/// <summary> /// Update the room /// </summary> public async Task <ServiceResult> UpdateAsync(int id, RoomRequestModel model) { try { Room room = await _roomRepository.Table.AsNoTracking() .FirstOrDefaultAsync(e => e.Id == id); if (room == null) { return(ServiceResult.Error(ErrorMessages.NotFound, HttpStatusCode.NotFound)); } var local = _roomRepository.Context.Set <Room>().Local.FirstOrDefault(e => e.Id == id); if (local != null) { _roomRepository.Context.Entry(local).State = EntityState.Detached; } room.UpdateFields(model.Name); _roomRepository.Update(room); await _roomRepository.SaveChangesAsync(); return(ServiceResult.Success(room.Id)); } catch (Exception e) { return(ServiceResult.Error(e.Message, HttpStatusCode.InternalServerError)); } }
/// <summary> /// Add a room /// </summary> public async Task <ServiceResult> AddAsync(RoomRequestModel model) { try { if (model == null || string.IsNullOrWhiteSpace(model.Name)) { return(ServiceResult.Error(ErrorMessages.InvalidModel, HttpStatusCode.BadRequest)); } if (_roomRepository.Table.Any(e => e.Name == model.Name)) { return(ServiceResult.Error(ErrorMessages.InvalidModel, HttpStatusCode.UnprocessableEntity)); } Room room = new Room(model.Name); _roomRepository.Add(room); await _roomRepository.SaveChangesAsync(); return(ServiceResult.Success(room.Id)); } catch (Exception e) { return(ServiceResult.Error(e.Message, HttpStatusCode.InternalServerError)); } }
public async Task <ServiceResponse <RoomModel> > PostAsync(RoomRequestModel roomRequestModel) { try { var room = await _context.People.AsNoTracking().FirstOrDefaultAsync(f => f.Name == roomRequestModel.Name); if (room != null) { return(new ServiceResponse <RoomModel> { ErrorCode = HttpStatusCode.Conflict, ErrorDescription = "Room already exists" }); } var newRoom = _mapper.Map <Room>(roomRequestModel); _context.Entry(newRoom).State = EntityState.Added; if (await _context.SaveChangesAsync() > 0) { return(new ServiceResponse <RoomModel> { Data = _mapper.Map <RoomModel>(newRoom) }); } return(new ServiceResponse <RoomModel> { ErrorCode = HttpStatusCode.InternalServerError, ErrorDescription = "Internal Server Error" }); } catch (Exception e) { Debug.WriteLine(e); return(new ServiceResponse <RoomModel> { ErrorCode = HttpStatusCode.InternalServerError, ErrorDescription = "Internal Server Error" }); } }