public async Task <RoomInfo> Create(RoomInfo room) { string tempName = room.Name; // make sure room id and name is unique var CheckRoomIdExist = await _repository.Room.FindAsync(room.Id); var CheckRoomNameExist = await _repository.Room.FirstOrDefaultAsync(e => e.Name == tempName); RoomInfo ErrorRoom = new RoomInfo { Id = room.Id, Name = "Error", }; if (CheckRoomIdExist == null && CheckRoomNameExist == null) { _repository.Room.Add(new Room { Id = room.Id, Name = room.Name }); var numberOfItemsCreated = await _repository.SaveChangesAsync(); if (numberOfItemsCreated == 1) { return(room); } else { return(ErrorRoom); } } else { if (CheckRoomIdExist != null) { ErrorRoom.Name = "Room id already exist"; } if (CheckRoomNameExist != null) { ErrorRoom.Name = "Room Name already exist. Room Name must be unique"; } if (CheckRoomIdExist != null && CheckRoomNameExist != null) { ErrorRoom.Name = "Room Id and Name already exist"; } return(ErrorRoom); } }
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 <PersonInfo> Create(PersonInfo person) { _repository.People.Add(new Data.Domain.Person { Id = person.Id, Name = person.Name, DateOfBirth = person.DateOfBirth }); var numberOfItemsCreated = await _repository.SaveChangesAsync(); if (numberOfItemsCreated == 1) { return(person); } else { PersonInfo ErrorAddPerson = new PersonInfo { Id = person.Id, Name = "Error", DateOfBirth = "Error" }; return(ErrorAddPerson); } }
public async Task <RoomBookingInfo> Create(RoomBookingInfo roomBooking) { var CheckBookingIdExist = await _repository.RoomBookings.FindAsync(roomBooking.Id); // Get the Person detail in Person Table with Id Person getPersonDetail = new Person(); getPersonDetail = _repository.People.FirstOrDefault(z => z.Name == roomBooking.PersonName && z.DateOfBirth == roomBooking.PersonDOB); // Get the room detail in Room Table with Id Room getRoomDetail = new Room(); getRoomDetail = _repository.Room.FirstOrDefault(y => y.Name == roomBooking.RoomName); // Pass error message RoomBookingInfo ErrorRoom = new RoomBookingInfo { Id = roomBooking.Id, }; if (CheckBookingIdExist == null) { if (getPersonDetail == null) { ErrorRoom.BookingNote = "Error in creating record. Person record does not exist"; return(ErrorRoom); } if (getRoomDetail == null) { ErrorRoom.BookingNote = "Error in creating record. Room Name does not exist"; return(ErrorRoom); } else { roomBooking.BookingDateTimeEnd = roomBooking.BookingDateTimeStart.AddMinutes(roomBooking.lengthBookingMin); _repository.RoomBookings.Add(new RoomBooking { Id = roomBooking.Id, PersonId = getPersonDetail.Id, RoomId = getRoomDetail.Id, BookingDateTimeStart = roomBooking.BookingDateTimeStart, BookingDateTimeEnd = roomBooking.BookingDateTimeEnd, lengthBookingMin = roomBooking.lengthBookingMin, BookingNote = roomBooking.BookingNote }); var numberOfItemsCreated = await _repository.SaveChangesAsync(); if (numberOfItemsCreated == 1) { return(roomBooking); } else { ErrorRoom.BookingNote = "Error in creating record"; return(ErrorRoom); } } } else { ErrorRoom.BookingNote = "Booking id already exist"; return(ErrorRoom); } }