public async Task <IActionResult> AddToShelve([FromBody] ShelveCreateDTO createShelveDTO)
        {
            ShelveDTO result;

            try {
                result = await _shelvesService.PostShelve(createShelveDTO);
            }
            catch (AlreadyInDBException e) {
                Console.WriteLine(e);
                return(Problem("AlreadyInDBException: " + e.Message));
            }
            catch (NotFoundException e) {
                Console.WriteLine(e);
                return(NotFound("NotFoundException: " + e.Message));
            }
            catch (BadRequestException e) {
                Console.WriteLine(e);
                return(BadRequest("BadRequestException: " + e.Message));
            }
            catch (Exception e) {
                Console.WriteLine(e);
                return(Problem("Exception: " + e.Message));
            }
            return(Created("/shelve/add", result));
        }
        public async Task <IActionResult> UpdateToShelve(string id, [FromBody] ShelveCreateDTO createShelveDTO)
        {
            ShelveDTO result;

            try {
                result = await _shelvesService.PutShelve(id, createShelveDTO);
            }
            catch (AlreadyInDBException e) {
                Console.WriteLine(e);
                return(Problem("AlreadyInDBException: " + e.Message));
            }
            catch (NotFoundException e) {
                Console.WriteLine(e);
                return(NotFound("NotFoundException: " + e.Message));
            }
            catch (BadRequestException e) {
                Console.WriteLine(e);
                return(BadRequest("BadRequestException: " + e.Message));
            }
            catch (Exception e) {
                Console.WriteLine(e);
                return(Problem("Exception: " + e.Message));
            }
            return(Ok(result));
        }
Beispiel #3
0
 /// <summary>
 /// Update.
 /// </summary>
 /// <param name="id"></param>
 /// <param name="updatedShelve"></param>
 public async Task <ShelveDTO> PutShelve(string id, ShelveCreateDTO updatedShelve)
 {
     if (id != updatedShelve.Id)
     {
         throw new BadRequestException("the id '" + id + "' is different of the DTO (dto id : '" + updatedShelve.Id + "') .");
     }
     return(await _shelvesRepository.UpdateShelve(id, _mapper.Map <ShelveCollection>(updatedShelve)));
 }
Beispiel #4
0
        /// <summary>
        /// Add.
        /// </summary>
        /// <param name="shelve"></param>
        public async Task <ShelveDTO> PostShelve(ShelveCreateDTO shelve)
        {
            try
            {
                if (shelve.BookIds is null || shelve.BookIds.Count() == 0)
                {
                    throw new ArgumentNullException("bookIds is null");
                }

                return(await _shelvesRepository.AddShelve(_mapper.Map <ShelveCollection>(shelve)));
            }
            catch {
                throw;
            }
        }
Beispiel #5
0
        /// <summary>
        /// Consutructor.
        /// </summary>
        /// <param name="id"></param>
        public async Task <ShelveDTO> AddBookToShelve(string bookId, string shelveId)
        {
            var shelve = await _shelvesRepository.GetShelveById(shelveId);

            var bookList = shelve.Books.Select(b => b.Id).ToList();

            bookList.Add(bookId);
            var updatedShelve = new ShelveCreateDTO()
            {
                Id      = shelveId,
                Name    = shelve.Name,
                BookIds = bookList
            };
            var result = await _shelvesRepository.UpdateShelve(shelveId, _mapper.Map <ShelveCollection>(updatedShelve));

            return(result);
        }
Beispiel #6
0
        /// <summary>
        /// Consutructor.
        /// </summary>
        /// <param name="id"></param>
        public async Task <ShelveDTO> DeleteBookFromShelve(string bookId, string shelveId)
        {
            var shelve = await _shelvesRepository.GetShelveById(shelveId);

            var bookList = shelve.Books.Select(b => b.Id).ToList();
            var index    = shelve.Books.ToList().FindIndex(b => b.Id == bookId);

            bookList.RemoveAt(index);
            var updatedShelve = new ShelveCreateDTO()
            {
                Id      = shelveId,
                Name    = shelve.Name,
                BookIds = bookList
            };
            var result = await _shelvesRepository.UpdateShelve(shelveId, _mapper.Map <ShelveCollection>(updatedShelve));

            return(result);
        }