Beispiel #1
0
        public async Task <IActionResult> Create([FromBody] AuthorEditModel author)
        {
            if (ModelState.IsValid)
            {
                AuthorResultModel response = await this.authorService.UpdateAuthor(author.Id, author.Name, author.ImageUrl, author.Website, author.Description, author.Books);

                if (!response.Success)
                {
                    FailedResponseModel badResponse = new FailedResponseModel()
                    {
                        Errors = response.Errors
                    };

                    return(BadRequest(badResponse));
                }

                AuthorSuccessResponseModel successResponse = new AuthorSuccessResponseModel()
                {
                    Name = response.Name
                };

                return(Ok(successResponse));
            }

            return(BadRequest(new FailedResponseModel {
                Errors = ModelState.Values.SelectMany(x => x.Errors.Select(y => y.ErrorMessage))
            }));
        }
Beispiel #2
0
        public async Task <AuthorResultModel> CreateAuthor(string name, string imageUrl, string website, string description, IEnumerable <GenericComboBox> books)
        {
            AuthorResultModel result = new AuthorResultModel();

            if (await this.dbContext.Authors.AnyAsync(p => p.Name.ToLower().Trim() == name.ToLower().Trim()))
            {
                result.Success = false;
                result.Errors  = new[] { "Author with this name already exist." };
                return(result);
            }

            Author author = new Author
            {
                Id          = Guid.NewGuid(),
                Name        = name.Trim(),
                Description = description.Trim(),
                ImageUrl    = imageUrl.Trim(),
                Website     = website.Trim(),
            };

            await this.dbContext.Authors.AddAsync(author);

            if (books != null && books.Any())
            {
                IEnumerable <Guid> bookIds = books.Where(x => x != null && x.Id != default).Select(x => x.Id);

                IEnumerable <Book> booksForUpdate = await this.dbContext.Books.Where(x => bookIds.Contains(x.Id)).ToListAsync();

                this.dbContext.Books.AttachRange(booksForUpdate);
                foreach (Book book in booksForUpdate)
                {
                    book.AuthorId = author.Id;
                }
            }

            await this.dbContext.SaveChangesAsync();

            result.Name    = author.Name;
            result.Success = true;

            return(result);
        }
Beispiel #3
0
        public async Task <IActionResult> Delete(Guid id)
        {
            AuthorResultModel response = await this.authorService.DeleteAuthor(id);

            if (!response.Success)
            {
                FailedResponseModel badResponse = new FailedResponseModel()
                {
                    Errors = response.Errors
                };

                return(BadRequest(badResponse));
            }

            AuthorSuccessResponseModel successResponse = new AuthorSuccessResponseModel()
            {
                Name = response.Name
            };

            return(Ok(successResponse));
        }
Beispiel #4
0
        public async Task <AuthorResultModel> DeleteAuthor(Guid id)
        {
            AuthorResultModel result = new AuthorResultModel();

            Author author = await this.dbContext.Authors.FirstOrDefaultAsync(p => p.Id == id);

            if (author == null)
            {
                result.Success = false;
                result.Errors  = new[] { "Author with this Id not exist." };
                return(result);
            }

            this.dbContext.Authors.Remove(author);

            await this.dbContext.SaveChangesAsync();

            result.Name    = author.Name;
            result.Success = true;

            return(result);
        }
Beispiel #5
0
        public async Task <AuthorResultModel> UpdateAuthor(Guid id, string name, string imageUrl, string website, string description, IEnumerable <GenericComboBox> books)
        {
            AuthorResultModel result = new AuthorResultModel();

            Author author = await this.dbContext.Authors.Include(x => x.Books).FirstOrDefaultAsync(p => p.Id == id);

            if (author == null)
            {
                result.Success = false;
                result.Errors  = new[] { "Author with this Id not exist." };
                return(result);
            }

            if (author.Name.ToLower().Trim() != name.ToLower().Trim() && await this.dbContext.Categories.AnyAsync(p => p.Name.ToLower().Trim() == name.ToLower().Trim() && p.Id != id))
            {
                result.Success = false;
                result.Errors  = new[] { "Author with this name already exist." };
                return(result);
            }

            this.dbContext.Authors.Attach(author);

            author.Name        = name.Trim();
            author.Description = description.Trim();
            author.ImageUrl    = imageUrl.Trim();
            author.Website     = website.Trim();

            await this.UpdateBooksAsync(books, author);

            await this.dbContext.SaveChangesAsync();

            result.Name    = author.Name;
            result.Success = true;

            return(result);
        }