예제 #1
0
        public OperationResult <BookResponse> Execute(BookRequest book)
        {
            try
            {
                if (_context.Books.FirstOrDefault(x => x.Id == book.Id) == null)
                {
                    _context.Books.Add(_bookMapper.Map(book));
                    _context.SaveChanges();
                    _context.Authors.Add(_authorBookMapper.Map(book));
                    _context.SaveChanges();

                    return(new OperationResult <BookResponse>()
                    {
                        IsSuccess = true
                    });
                }
                else
                {
                    throw new ArgumentException("This id already exists");
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
                return(new OperationResult <BookResponse>()
                {
                    IsSuccess = false,
                    ErrorMessages = { exc.Message }
                });
            }
        }
예제 #2
0
        public OperationResult <BookResponse> Execute(
            BookRequest book)
        {
            try
            {
                DbBook       oldBook    = _context.Books.FirstOrDefault(x => x.Id == book.Id);
                DbAuthorBook authorBook = _context.Authors.FirstOrDefault(x => x.BookId == book.Id);

                if (oldBook == null)
                {
                    throw new ArgumentException("Old model doesn't exist");
                }

                _context.Books.Remove(oldBook);
                _context.Books.Add(_dbBookMapper.Map(book));
                if (authorBook != null)
                {
                    _context.Authors.Remove(authorBook);
                }

                _context.Authors.Add(_dbAuthorBookMapper.Map(book));
                _context.SaveChanges();

                return(new OperationResult <BookResponse>()
                {
                    IsSuccess = true
                });
            }
            catch (Exception exc)
            {
                return(new OperationResult <BookResponse>()
                {
                    IsSuccess = false,
                    ErrorMessages = { exc.Message }
                });
            }
        }