private Book MapToRequest(BookRequestDto book) { if (book != null) { return(new Book { Id = book.Id, Slug = book.Slug, Author = book.Author, Available2Lend = book.Available2Lend, Description = book.Description, Name = book.Name, Language = book.Language, ISBN = book.ISBN, ISBN13 = book.ISBN13, Dimensions = book.Dimensions, Length = book.Length, Weight = book.Weight, PublishedDate = book.PublishedDate, ImageUrl = book.ImageUrl, BookCategory = _GetCatById(book.CatId) }); } return(null); }
public async Task <string> RezerwujPotwierdz(BookRequestDto book) { var values = new Dictionary <string, string> { { "action", "Rezerwacje4Datepicker" }, { "klie_nick", "tyborowskif" }, { "data_od", "" }, { "data_do", "" } }; var content = new FormUrlEncodedContent(values); HttpResponseMessage response; try { response = await client.PostAsync("http://hastalavista.pl/wp-admin/admin-ajax.php", content); response.EnsureSuccessStatusCode(); } catch (Exception ex) { throw new Exception(ex.Message); } return(await response.Content.ReadAsStringAsync()); }
public async Task <long> CreateAsync(BookRequestDto dto) { dto.Sanitize(); var book = Mapper.Map <Book>(dto); book.SetNewId(); // 处理标签 foreach (var tname in dto.Tags) { var tag = await _tagRepository.GetByName(tname); if (tag == null) { tag = new Tag() { Name = tname } } ; book.BookTags.Add(new BookTag { Book = book, Tag = tag }); } Context.Add(book); await Context.SaveChangesAsync(); return(book.Id); }
/// <summary> /// Updates the specified book. /// </summary> /// <param name="id">The book identifier.</param> /// <param name="requestDto">The request dto.</param> /// <returns>Updated book</returns> public async Task <BookResponseDto> Update(int id, BookRequestDto requestDto, string uploadedImageUrl) { BookResponseDto book = null; SqlConnection connection = _dBContext.GetConnection(); SqlCommand command = new SqlCommand("sp_books_update", connection) { CommandType = System.Data.CommandType.StoredProcedure }; command.Parameters.AddWithValue("@id", id); command.Parameters.AddWithValue("@description", requestDto.Description); command.Parameters.AddWithValue("@author", requestDto.Author); command.Parameters.AddWithValue("@title", requestDto.Title); command.Parameters.AddWithValue("@image", uploadedImageUrl); command.Parameters.AddWithValue("@price", requestDto.Price); command.Parameters.AddWithValue("@quantity", requestDto.Quantity); await connection.OpenAsync(); using (SqlDataReader reader = await command.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { book = MapReaderTobook(reader); } } await connection.CloseAsync(); return(book); }
public async Task <string> Rezerwuj(BookRequestDto book) { var values = new Dictionary <string, string> { { "action", "RezerwujWybraneZapisz" }, { "data", $"{book.Data.ToString("yyyy-MM-dd")}" }, { "REZ[]", $"{book.Rez[0]}" } }; var content = new FormUrlEncodedContent(values); HttpResponseMessage response; try { response = await client.PostAsync("http://hastalavista.pl/wp-admin/admin-ajax.php", content); response.EnsureSuccessStatusCode(); } catch (Exception ex) { throw new Exception(ex.Message); } return(await response.Content.ReadAsStringAsync()); }
public async Task CreateBook_ValidBook_Success() { // Arrange var bookRequestDtos = new BookRequestDto { Id = 0, Name = "Book 1", CatId = 1, ISBN = "1213244234" }; var bookResponseDtos = new BookResponseDto { Id = 100, Name = "Book 1", CatId = 1, ISBN = "1213244234" }; _bookServiceMock.Setup(c => c.CreateAsync(bookRequestDtos)).ReturnsAsync(bookResponseDtos); _booksApiController = new BooksApiController(_bookServiceMock.Object); // Act var result = await _booksApiController.Post(bookRequestDtos); // Assert var resultStatusCode = ((Microsoft.AspNetCore.Mvc.ObjectResult)result).StatusCode; var resultValue = (BookResponseDto)((Microsoft.AspNetCore.Mvc.ObjectResult)result).Value; Assert.Equal((int)HttpStatusCode.Created, resultStatusCode); Assert.Equal(bookResponseDtos, resultValue); }
public void UpdateBook_ValidBook_Success() { // Arrange var bookRequestDtos = new BookRequestDto { Id = 100, Name = "Book 2", CatId = 1, ISBN = "1213244234", PublishedDate = new DateTime(2019, 10, 1) }; var bookResponseDtos = new BookResponseDto { Id = 100, Name = "Book 2", CatId = 1, ISBN = "1213244234", PublishedDate = new DateTime(2019, 11, 1) }; _bookServiceMock.Setup(c => c.Update(bookRequestDtos.Id, bookRequestDtos)).Returns(bookResponseDtos); _booksApiController = new BooksApiController(_bookServiceMock.Object); // Act var result = _booksApiController.Put(bookRequestDtos.Id, bookRequestDtos); // Assert var resultStatusCode = ((Microsoft.AspNetCore.Mvc.ObjectResult)result).StatusCode; var resultValue = ((Microsoft.AspNetCore.Mvc.ObjectResult)result).Value; Assert.Equal((int)HttpStatusCode.Accepted, resultStatusCode); Assert.Equal(bookResponseDtos, resultValue); }
public async Task <IActionResult> Post([FromBody] BookRequestDto book) { if (!ModelState.IsValid) { return(BadRequest()); } return(Created(nameof(Get), await _bookService.CreateAsync(book))); }
public void Rezwruj_Shoul_Book_Specified_COurts() { string[] rez = new string[] { "4_14:00_14:30", "4_14:30_15:00" }; var rezwrujRequest = new BookRequestDto() { Data = DateTime.Now, Login = "******", Password = "******", Rez = rez }; var response = squashService.RezerwujTest(rezwrujRequest); }
public IActionResult Put([FromRoute] int id, [FromBody] BookRequestDto book) { if (!ModelState.IsValid) { return(BadRequest()); } var result = _bookService.Update(id, book); return(Accepted(result)); }
public async Task <IActionResult> AddBook([FromForm] BookRequestDto requestDto) { string email = (string)HttpContext.Items["email"]; BookResponseDto responseDto = await _service.AddBook(requestDto, email); return(Ok(new Response <BookResponseDto> { StatusCode = (int)HttpStatusCode.OK, Message = ResponseMessage.BOOK_ADDED, Data = responseDto })); }
public async Task PostAsync_ReturnCreatedBook() { var requestDto = new BookRequestDto(); var responseDto = new BookResponseDto(); _booksService.Setup(bs => bs.PostAsync(requestDto)).ReturnsAsync(responseDto); var result = await _booksController.PostAsync(requestDto); Assert.That(result.Value, Is.EqualTo(responseDto)); _booksService.Verify(bs => bs.PostAsync(requestDto)); }
private void SetModelToUI(BookRequestDto dto) { DataFilled.Value = dto.Filled; PriceNum.Value = dto.Price; DeliveryData.Value = dto.Delivery; FullNameBox.Text = dto.FullName; AdressBox.Text = dto.Addres; BookList.Items.Clear(); foreach (var item in dto.BookDetails) { BookList.Items.Add(item); } }
public OperationResult <BookInfoDto> AddBooking(BookRequestDto bookRequestDto) //operation result . if data is null then not avaible. book oluşturulduğunda ekrana info sayfasını vercek { if (CarRepository.IsAvaible(bookRequestDto.carId, bookRequestDto.RentStartDate, bookRequestDto.RentEndDate)) { var book = CreateBooking(bookRequestDto); BookRepository.AddAndSave(book); return(new OperationResult <BookInfoDto>(true, "Successfully booked", (int)HttpStatusCode.OK, book.ToBookInfoDto())); } else { return(new OperationResult <BookInfoDto>(false, "These dates are not available for the selected car", (int)HttpStatusCode.NotFound, null)); } }
public async Task PostAsync_ValidObject_ReturnNewBook() { var newBook = new BookRequestDto(); _booksRepository.Setup(br => br.AnyAsync(It.IsAny <Expression <Func <Book, bool> > >())) .ReturnsAsync(false); var result = await _booksService.PostAsync(newBook); Assert.True(result.Title == newBook.Title && result.Author == newBook.Author); _unitOfWork.Verify(uow => uow.SaveAsync()); _booksRepository.Verify(br => br.AnyAsync(It.IsAny <Expression <Func <Book, bool> > >())); _booksRepository.Verify(br => br.AddAsync(It.IsAny <Book>())); }
public void PutAsync_DuplicateTitle_Throw() { var bookId = Guid.NewGuid(); var newBook = new BookRequestDto(); var book = new Book(); _booksRepository.Setup(br => br.GetAsync(bookId)).ReturnsAsync(book); _booksRepository.Setup(br => br.AnyAsync(It.IsAny <Expression <Func <Book, bool> > >())) .ReturnsAsync(true); Assert.ThrowsAsync <DuplicateBookException>(async() => await _booksService.PutAsync(bookId, newBook)); _booksRepository.Verify(br => br.GetAsync(bookId)); _unitOfWork.Verify(uow => uow.SaveAsync(), Times.Never); }
public IActionResult BookCourt([FromBody] BookRequestDto book) { if (book == null) { return(BadRequest("Request was empty")); } var response = _squash.RezerwujTest(book); if (response.StatusCode == System.Net.HttpStatusCode.OK) { return(Ok(response.Content)); } else { return(BadRequest(response.Content)); } }
/// <summary> /// Performs business logic while adding book. /// </summary> /// <param name="requestDto">The request dto.</param> /// <returns>BookResponseDto object</returns> /// <exception cref="BookstoreException">Invalid data</exception> public async Task <BookResponseDto> AddBook(BookRequestDto requestDto, string email) { try { string uploadedImageUrl = await _cloudService.UpdloadToCloud(requestDto.Image, email); int id = await _repository.Insert(requestDto, uploadedImageUrl); BookResponseDto response = _mapper.Map <BookResponseDto>(requestDto); response.Id = id; response.Image = uploadedImageUrl; return(response); } catch (SqlException e) when(e.Number == SqlErrorNumbers.CONSTRAINT_VOILATION) { throw new BookstoreException("Invalid data"); } }
public async Task <BookResponseDto> CreateAsync(BookRequestDto book) { if (!string.IsNullOrWhiteSpace(book.Slug)) { book.Slug = FriendlyUrlHelper.GetFriendlyTitle(book.Slug, true); } else { book.Slug = FriendlyUrlHelper.GetFriendlyTitle(book.Name, true); } var bookEntity = MapToRequest(book); bookEntity.Slug = await _bookRepository.SafeToSlug(bookEntity.Slug, bookEntity.Id); var result = await _bookRepository.AddAsync(bookEntity); return(MapToResponse(result)); }
private Book CreateBooking(BookRequestDto bookRequestDto) { var car = CarRepository.GetById(bookRequestDto.carId); int?customerId; if (CustomerRepository.IsCustomerExist(bookRequestDto.CustomerEmail)) { customerId = CustomerRepository.GetCustomerId(bookRequestDto.CustomerEmail); } else { List <CustomerProperties> customerProperties = new List <CustomerProperties> { new CustomerProperties { TypeId = 1, Value = bookRequestDto.CustomerAge }, new CustomerProperties { TypeId = 2, Value = bookRequestDto.CustomerLicenseAge } }; var customer = new Customer //TODO: check customer exist { CustomerEmail = bookRequestDto.CustomerEmail, CustomerPhoneNumber = bookRequestDto.CustomerPhoneNumber, CustomerName = bookRequestDto.CustomerName, CustomerProperties = customerProperties }; CustomerRepository.AddAndSave(customer); customerId = customer.Id; } var book = new Book { RentStartDate = bookRequestDto.RentStartDate, RentEndDate = bookRequestDto.RentEndDate, ReferenceNumber = CreateReferenceNumber(), BeforeKm = car.CarKm, AfterKm = car.CarKm, CustomerId = (int)customerId, CarId = car.Id }; return(book); }
public async Task <BookResponseDto> PostAsync(BookRequestDto requestDto) { var bookExists = await _unitOfWork.BooksRepository.AnyAsync(b => b.Title == requestDto.Title); if (bookExists) { throw new DuplicateBookException(requestDto.Title); } var book = requestDto.Adapt <BookRequestDto, Book>(); await _unitOfWork.BooksRepository.AddAsync(book); await _unitOfWork.SaveAsync(); var responseDto = book.Adapt <Book, BookResponseDto>(); return(responseDto); }
public void TestMethod() { var dto = new BookRequestDto { Filled = DateTime.Now, FullName = "Тот чье имя нельзя называть", BookTitles = new List <BookTitle>() { new BookTitle { Book = new List <BookDetails>() { new BookDetails { Title = "Гарри Поттер. Все части", Genre = Genre.HumorousLiterature, PagesNumber = 322, Price = 40000, AgeLimit = AgeLimit.sixAndOlder, Presence = true } } } }, Addres = "Косой переулок", Price = 40000, Currency = Currency.Bitcoins }; var tempFileName = Path.GetTempFileName(); try { BooksDtoHelper.WriteToFile(tempFileName, dto); var readDto = BooksDtoHelper.LoadFromFile(tempFileName); Assert.AreEqual(dto.FullName, readDto.FullName); Assert.AreEqual(dto.Currency, readDto.Currency); } finally { File.Delete(tempFileName); } }
/// <summary> /// Performs business logic while updating the specified book. /// </summary> /// <param name="id">The book identifier.</param> /// <param name="requestDto">The request dto.</param> /// <returns>book information</returns> /// <exception cref="BookstoreException">Invalid data</exception> public async Task <BookResponseDto> Update(int id, BookRequestDto requestDto, string email) { try { string uploadedImageUrl = null; if (requestDto.Image != null) { uploadedImageUrl = await _cloudService.UpdloadToCloud(requestDto.Image, email); } return(await _repository.Update(id, requestDto, uploadedImageUrl)); } catch (SqlException e) when(e.Number == SqlErrorNumbers.CONSTRAINT_VOILATION) { throw new BookstoreException("Invalid data"); } catch (SqlException e) when(e.Number == 50000) { throw new BookstoreException(e.Message); } }
public async Task PutAsync_ValidObject_ReturnUpdatedProduct() { var bookId = Guid.NewGuid(); var newBook = new BookRequestDto { Author = "Josh", Title = "Nature" }; var book = new Book(); _booksRepository.Setup(br => br.GetAsync(bookId)).ReturnsAsync(book); _booksRepository.Setup(br => br.AnyAsync(It.IsAny <Expression <Func <Book, bool> > >())) .ReturnsAsync(false); var result = await _booksService.PutAsync(bookId, newBook); Assert.True(result.Title == newBook.Title && result.Author == newBook.Author); _booksRepository.Verify(br => br.GetAsync(bookId)); _unitOfWork.Verify(uow => uow.SaveAsync()); }
public BookResponseDto Update(int id, BookRequestDto book) { var bookEntity = _GetById(id); bookEntity.Author = book.Author; bookEntity.Available2Lend = book.Available2Lend; bookEntity.Description = book.Description; bookEntity.Name = book.Name; bookEntity.Language = book.Language; bookEntity.PublishedDate = book.PublishedDate; bookEntity.ISBN = book.ISBN; bookEntity.ISBN13 = book.ISBN13; bookEntity.Weight = book.Weight; bookEntity.Length = book.Length; bookEntity.Dimensions = book.Dimensions; bookEntity.ImageUrl = book.ImageUrl; _bookRepository.Update(bookEntity); return(MapToResponse(bookEntity)); }
public async Task UpdateAsync(long id, BookRequestDto dto) { dto.Sanitize(); var book = await _bookRepository.GetByIdAsync(id, q => q.Include(x => x.BookTags).Include(x => x.Links)); Mapper.Map(dto, book); // 处理标签,先移除标签与图书的关联再添加 Context.RemoveRange(book.BookTags); foreach (var tname in dto.Tags) { var tag = await _tagRepository.GetByName(tname); if (tag == null) { tag = new Tag() { Name = tname } } ; book.BookTags.Add(new BookTag { Book = book, Tag = tag }); } // 处理下载链接,移除已删除项 var oldLinks = await _linkRepository.GetByBookIdAsync(id); foreach (var oldLink in oldLinks) { if (!book.Links.Any(x => x.Id == oldLink.Id)) { Context.Remove(oldLink); } } await Context.SaveChangesAsync(); }
/// <summary> /// Inserts the book into database. /// </summary> /// <param name="requestDto">The request dto.</param> /// <returns>Boolean result in format 1/0</returns> public async Task <int> Insert(BookRequestDto requestDto, string imageUrl) { SqlConnection connection = _dBContext.GetConnection(); SqlCommand command = new SqlCommand("sp_books_insert", connection) { CommandType = System.Data.CommandType.StoredProcedure }; command.Parameters.AddWithValue("@description", requestDto.Description); command.Parameters.AddWithValue("@title", requestDto.Title); command.Parameters.AddWithValue("@author", requestDto.Author); command.Parameters.AddWithValue("@image", imageUrl); command.Parameters.AddWithValue("@price", requestDto.Price); command.Parameters.AddWithValue("@quantity", requestDto.Quantity); await connection.OpenAsync(); int id = Convert.ToInt32(await command.ExecuteScalarAsync()); await connection.CloseAsync(); return(id); }
public async Task <IActionResult> UpdateBook(int id, [FromForm] BookRequestDto requestDto) { string email = (HttpContext.Items["email"]).ToString(); BookResponseDto book = await _service.Update(id, requestDto, email); if (book == null) { return(NotFound(new Response <BookResponseDto> { StatusCode = (int)HttpStatusCode.NotFound, Message = ResponseMessage.BOOK_NOT_FOUND, Data = book })); } return(Ok(new Response <BookResponseDto> { StatusCode = (int)HttpStatusCode.OK, Message = ResponseMessage.BOOK_UPDATED, Data = book })); }
public void addBooking() { BookRequestDto bookCustomerDto = new BookRequestDto { CustomerAge = 22, CustomerEmail = "*****@*****.**", CustomerLicenseAge = 7, CustomerName = "yasin", CustomerPhoneNumber = "05434730833", RentStartDate = new DateTime(2017, 1, 15), RentEndDate = new DateTime(2017, 1, 18), carId = 1 }; _bookService.AddBooking(bookCustomerDto); bookCustomerDto = new BookRequestDto { CustomerAge = 22, CustomerEmail = "*****@*****.**", CustomerLicenseAge = 7, CustomerName = "selin", CustomerPhoneNumber = "05434730833", RentStartDate = new DateTime(2019, 1, 5), RentEndDate = new DateTime(2019, 1, 8), carId = 2 }; _bookService.AddBooking(bookCustomerDto); bookCustomerDto = new BookRequestDto { CustomerAge = 22, CustomerEmail = "*****@*****.**", CustomerLicenseAge = 7, CustomerName = "cem", CustomerPhoneNumber = "05434730833", RentStartDate = new DateTime(2018, 12, 15), RentEndDate = new DateTime(2019, 1, 30), carId = 1 }; _bookService.AddBooking(bookCustomerDto); }
public async Task <BookResponseDto> PutAsync(Guid bookId, BookRequestDto requestDto) { var bookFromDb = await _unitOfWork.BooksRepository.GetAsync(bookId); if (bookFromDb == null) { throw new BookNotFoundException(bookId); } var titleExists = await _unitOfWork.BooksRepository.AnyAsync(b => b.Title == requestDto.Title && b.Id != bookId); if (titleExists) { throw new DuplicateBookException(requestDto.Title); } requestDto.Adapt(bookFromDb); await _unitOfWork.SaveAsync(); var responseDto = bookFromDb.Adapt <Book, BookResponseDto>(); return(responseDto); }