public void Add_ThrowExceptionIfOblastWithGivenIdDoNotExist() { var model = new HotelAddViewModel { SelectedOblastId = 1 }; Action testCode = () => this.hotelsService.Add(model); testCode.ShouldThrow <OblastNullException>(); }
public void Add_ShouldCreateNewHotelCorrectly() { string address = "testAddress"; string description = "testDescription"; string name = "testName"; string phoneNumber = "testPhoneNumner"; int stars = 1; int oblastId = 1; this.dbContext.Oblasts.Add(new Oblast { Id = oblastId }); this.dbContext.SaveChanges(); var model = new HotelAddViewModel { Address = address, Description = description, Name = name, PhoneNumber = phoneNumber, Stars = stars, SelectedOblastId = oblastId, }; string imageUrl = "testUrl"; string imagesDirectory = "wwwroot/images/hotels/"; string imagesFolderName = "hotels"; var mockedImagesUploader = new Mock <ImagesUploader>(null); mockedImagesUploader .Setup(x => x.Upload(null, imagesDirectory, imagesFolderName)) .Returns(imageUrl); typeof(HotelsService) .GetFields(BindingFlags.NonPublic | BindingFlags.Instance) .First(x => x.FieldType == typeof(ImagesUploader)) .SetValue(this.hotelsService, mockedImagesUploader.Object); this.hotelsService.Add(model); Hotel result = this.dbContext.Hotels.First(); result.ShouldSatisfyAllConditions ( () => result.Address.ShouldBe(address), () => result.Description.ShouldBe(description), () => result.Name.ShouldBe(name), () => result.PhoneNumber.ShouldBe(phoneNumber), () => result.Stars.ShouldBe(stars), () => result.OblastId.ShouldBe(oblastId), () => result.ImageUrl.ShouldBe(imageUrl) ); }
public IActionResult Add() { var oblasts = this.oblastsService.GetAllOrderedByName() .Select(x => new SelectListItem(x.Name, x.Id.ToString())) .ToList(); var viewModel = new HotelAddViewModel { Oblasts = oblasts }; return(base.View(viewModel)); }
public IActionResult Add(HotelAddViewModel model) { if (!this.ModelState.IsValid) { return(base.RedirectToAction("Add")); } int hotelId = this.hotelsService.Add(model); return(base.RedirectToAction("Details", new { hotelId })); }
public int Add(HotelAddViewModel model) { if (!this.oblastsService.CheckExistById(model.SelectedOblastId)) { throw new OblastNullException(); } Hotel hotel = this.mapper.Map <Hotel>(model); hotel.ImageUrl = this.imagesUploader.Upload(model.Photo, ImagesDirectory, ImagesFolderName); this.dbContext.Hotels.Add(hotel); this.dbContext.SaveChanges(); return(hotel.Id); }
public async Task <IActionResult> Post([FromBody] HotelAddViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.Hotels.Add(new Hotel { Name = model.Name, Description = model.Description, Class = model.Class, //Price = model.Price, RoomsCount = model.RoomsCount, RegionId = model.RegionId }); await _context.SaveChangesAsync(); return(Ok(model)); }