public Room CreateRoom(CreateRoomInputModel input) { // Map input to new room var room = new Room { RoomName = input.RoomName, Users = new List <User>(), CreatedDateTime = DateTime.UtcNow, UpdatedDateTime = DateTime.UtcNow, }; // Generate unique short id for the RoomId var shortIdGenerationOptions = new GenerationOptions { Length = 8 }; do { room.RoomId = ShortId.Generate(shortIdGenerationOptions); } while (DbContext.Rooms.Any(r => r.RoomId == room.RoomId)); DbContext.Rooms.Add(room); DbContext.SaveChanges(); return(room); }
public async Task <IActionResult> Create(CreateRoomInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } await this.roomsService.CreateRoomAsync(input); this.TempData["InfoMessage"] = GlobalConstants.CreateRoomTempDataSuccess; return(this.RedirectToAction("All")); }
public async Task <bool> CreateRoomAsync(CreateRoomInputModel input) { string folderName = "room_images"; var pictureUrls = input.Pictures .Select(async x => await this.cloudinaryService.UploadPhotoAsync(x, x.FileName, folderName)) .Select(x => x.Result) .ToList(); var room = new Room { RoomType = input.RoomType, Price = input.Price, NumberOfBeds = input.NumberOfBeds, HasBathroom = input.HasBathroom, HasRoomService = input.HasRoomService, HasSeaView = input.HasSeaView, HasMountainView = input.HasMountainView, HasWifi = input.HasWifi, HasTv = input.HasTv, HasPhone = input.HasPhone, HasAirConditioner = input.HasAirConditioner, HasHeater = input.HasHeater, Pictures = pictureUrls.Select(x => new Picture { Url = x }) .ToList(), }; if (room != null && room.Price > 0) { await this.repository.AddAsync(room); var result = await this.repository.SaveChangesAsync(); return(true); } throw new InvalidOperationException(GlobalConstants.InvalidOperationExceptionForRoomCreate); }
public async Task AdministratorCreateRoom_WithValidData_ShouldReturnRoom() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; this.dbContext = new ApplicationDbContext(options); var roomRepository = new EfDeletableEntityRepository <Room>(this.dbContext); var roomReservationRepository = new EfDeletableEntityRepository <RoomReservation>(this.dbContext); var pictureRepository = new EfDeletableEntityRepository <Picture>(this.dbContext); var pictureService = new PictureService(pictureRepository); var moqCloudinaryService = new Mock <ICloudinaryService>(); var moqIFormFile = new Mock <IFormFile>(); this.roomService = new RoomsService(roomRepository, roomReservationRepository, pictureService, moqCloudinaryService.Object); var room = new CreateRoomInputModel { RoomType = (RoomType)Enum.Parse(typeof(RoomType), "SingleRoom"), Price = 10, Pictures = new List <IFormFile> { moqIFormFile.Object }, HasAirConditioner = true, HasBathroom = true, HasHeater = false, HasMountainView = false, HasPhone = false, HasRoomService = false, HasSeaView = false, HasTv = false, HasWifi = false, NumberOfBeds = 1, }; var actual = await this.roomService.CreateRoomAsync(room); Assert.True(actual); }
public IActionResult Create(CreateRoomInputModel roomInput) { var room = _roomService.CreateRoom(roomInput); return(RedirectToAction("Room", "Room", new { id = room.RoomId })); }