public async Task <string> Update(RoomBooking booking) { var ExistingRoomBooking = await _repository.RoomBookings.FindAsync(booking.Id); if (ExistingRoomBooking != null) { try { _repository.Entry(ExistingRoomBooking).State = EntityState.Detached; _repository.RoomBookings.Attach(booking); _repository.Entry(ExistingRoomBooking).State = EntityState.Modified; var numberOfItemsUpdated = await _repository.SaveChangesAsync(); if (numberOfItemsUpdated == 1) { return("Success"); } else { return("Error in updating booking record"); } } catch { throw; } } else { return("Booking record does not exist"); } }
public async Task <PersonInfo> Update(PersonInfo person) { var ExistingPerson = await _repository.People.FindAsync(person.Id); PersonInfo ErrorUpdatePerson = new PersonInfo { Id = person.Id, Name = "Error", DateOfBirth = "Error" }; if (ExistingPerson != null) { try { Person NewUpdatePersonDetail = new Person { Id = person.Id, Name = person.Name, DateOfBirth = person.DateOfBirth }; _repository.Entry(ExistingPerson).State = EntityState.Detached; _repository.People.Attach(NewUpdatePersonDetail); _repository.Entry(NewUpdatePersonDetail).State = EntityState.Modified; var numberOfItemsUpdated = await _repository.SaveChangesAsync(); if (numberOfItemsUpdated == 1) { return(person); } else { return(ErrorUpdatePerson); } } catch { throw; } } else { ErrorUpdatePerson.Name = "Record does not exist"; return(ErrorUpdatePerson); } }
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" }); } }
public async Task <ServiceResponse <PersonModel> > PostAsync(PersonRequestModel personRequestModel) { try { var person = await _context.People.AsNoTracking().FirstOrDefaultAsync(f => f.Name == personRequestModel.Name && f.DateOfBirth == personRequestModel.DateOfBirth); if (person != null) { return(new ServiceResponse <PersonModel> { ErrorCode = HttpStatusCode.Conflict, ErrorDescription = "Person already exists" }); } var newPerson = _mapper.Map <Person>(personRequestModel); _context.Entry(newPerson).State = EntityState.Added; if (await _context.SaveChangesAsync() > 0) { return(new ServiceResponse <PersonModel> { Data = _mapper.Map <PersonModel>(newPerson) }); } return(new ServiceResponse <PersonModel> { ErrorCode = HttpStatusCode.InternalServerError, ErrorDescription = "Internal Server Error" }); } catch (Exception e) { Debug.WriteLine(e); return(new ServiceResponse <PersonModel> { ErrorCode = HttpStatusCode.InternalServerError, ErrorDescription = "Internal Server Error" }); } }
public async Task <RoomInfo> Update(RoomInfo room) { var ExistingRoom = await _repository.Room.FindAsync(room.Id); RoomInfo ErrorUpdateRoom = new RoomInfo { Id = room.Id, Name = "Error", }; if (ExistingRoom != null) { try { Room NewUpdateRoomDetail = new Room(); string tempName = room.Name; //check if the new room name is unique var CheckRoomNameExist = _repository.Room.Where(e => e.Name == tempName && e.Id != room.Id).ToList().Count; if (CheckRoomNameExist != 1) { NewUpdateRoomDetail.Id = room.Id; NewUpdateRoomDetail.Name = room.Name; _repository.Entry(ExistingRoom).State = EntityState.Detached; _repository.Room.Attach(NewUpdateRoomDetail); _repository.Entry(NewUpdateRoomDetail).State = EntityState.Modified; var numberOfItemsUpdated = await _repository.SaveChangesAsync(); if (numberOfItemsUpdated == 1) { return(room); } else { return(ErrorUpdateRoom); } } else { ErrorUpdateRoom.Name = "Room Name already exist. New Room Name must be Unique"; return(ErrorUpdateRoom); } } catch { throw; } } else { ErrorUpdateRoom.Name = "Record does not exist"; return(ErrorUpdateRoom); } }