Example #1
0
        public int GetPageCount(AuthorFilterModel authorFilter = null, bool getRemoved = false)
        {
            int authorsCount = _authorRepository.GetCount(authorFilter, getRemoved);
            int pageCount    = (int)Math.Ceiling(authorsCount / (double)Constants.AUTHORPAGESIZE);

            return(pageCount);
        }
Example #2
0
        public List <AuthorModel> GetAuthorsFiltered(AuthorFilterModel authorFilter = null, string field = null, string ascending = Constants.DEFAULTSORTORDER, int page = Constants.DEFAULTPAGE, bool getRemoved = false)
        {
            var dbAuthors = _authorRepository.Get(authorFilter, field, ascending == Constants.DEFAULTSORTORDER, getRemoved, page);
            var authors   = _mapper.Map <List <AuthorModel> >(dbAuthors);

            return(authors);
        }
Example #3
0
        public int GetCount(AuthorFilterModel authorFilter = null, bool getRemoved = false)
        {
            Expression <Func <AuthorEntity, bool> > filter = null;

            if (authorFilter is not null)
            {
                filter = author => (string.IsNullOrWhiteSpace(authorFilter.Name) || author.Name.Contains(authorFilter.Name)) &&
                         (!authorFilter.EditionAuthors.Any() || authorFilter.EditionAuthors.Contains(author.Name));
            }
            return(base.Get(filter, getRemoved: getRemoved).Count());
        }
        public static FilterModel <DataAccess.Entities.Author> MapToEFFilterModel(this AuthorFilterModel filterBL)
        {
            var filterDAL = new FilterModel <DataAccess.Entities.Author>();

            filterDAL.SortProperty = filterBL.SortProperty;
            filterDAL.IsAscending  = filterBL.IsAscending;
            filterDAL.StartIndex   = filterBL.StartIndex;
            filterDAL.Quantity     = filterBL.Quantity;
            filterDAL.Predicate    = author => (string.IsNullOrWhiteSpace(filterBL.Name) || author.Name.ToLower().Contains(filterBL.Name.ToLower())) &&
                                     (!author.isRemoved);
            return(filterDAL);
        }
Example #5
0
        public void AddPrintingEdition(PrintingEditionModel printingEdition)
        {
            _validator.ValidatePrintingEdition(printingEdition);
            var dbPrintingEdition = _mapper.Map <PrintingEditionEntity>(printingEdition);

            dbPrintingEdition.Status = Enums.PrintingEditionStatusType.InStock;
            var authorFilter = new AuthorFilterModel {
                EditionAuthors = printingEdition.Authors
            };
            var authors = _authorRepository.GetAll(authorFilter);

            dbPrintingEdition.Authors = new List <AuthorEntity>(authors);
            _printingEditionRepository.Insert(dbPrintingEdition);
        }
Example #6
0
        public AuthorModel GetAuthorByName(string name)
        {
            var authorFilter = new AuthorFilterModel {
                Name = name
            };
            var dbAuthor = _authorRepository.Get(authorFilter).FirstOrDefault();

            if (dbAuthor is null)
            {
                throw new CustomApiException(HttpStatusCode.BadRequest, Constants.AUTHORNOTFOUNDERROR);
            }
            var author = _mapper.Map <AuthorModel>(dbAuthor);

            return(author);
        }
Example #7
0
        public List <AuthorEntity> Get(AuthorFilterModel authorFilter = null, string field = null, bool ascending = true, bool getRemoved = false, int page = Constants.DEFAULTPAGE)
        {
            page = page < Constants.DEFAULTPAGE ? Constants.DEFAULTPAGE : page;
            Expression <Func <AuthorEntity, bool> > filter = null;

            if (authorFilter is not null)
            {
                filter = author => (string.IsNullOrWhiteSpace(authorFilter.Name) || author.Name.Contains(authorFilter.Name)) &&
                         (!authorFilter.EditionAuthors.Any() || authorFilter.EditionAuthors.Contains(author.Name));
            }

            return(base.Get(filter, field, ascending, getRemoved)
                   .Skip((page - Constants.DEFAULTPREVIOUSPAGEOFFSET) * Constants.AUTHORPAGESIZE)
                   .Take(Constants.AUTHORPAGESIZE).ToList());
        }
Example #8
0
        public void AddAuthor(AuthorModel author)
        {
            _validator.ValidateAuthor(author);
            var authorFilter = new AuthorFilterModel {
                Name = author.Name
            };
            var dbAuthor = _authorRepository.Get(authorFilter).FirstOrDefault();

            if (dbAuthor is not null)
            {
                throw new CustomApiException(HttpStatusCode.UnprocessableEntity, Constants.AUTHORALREADYEXISTSERROR);
            }
            dbAuthor = _mapper.Map <AuthorEntity>(author);

            _authorRepository.Insert(dbAuthor);
        }
Example #9
0
        public async Task <(IEnumerable <Author>, int)> GetFilteredAuthorsAsync(AuthorFilterModel model, PaginationFilterModel pagination)
        {
            var result = await _dbSet
                         .Include(pe => pe.AuthorInPrintingEdition)
                         .ThenInclude(u => u.PrintingEdition)
                         .OrderBy($"{model.SortBy} {model.TypeSort}")
                         .Where(author => model.Name == null || EF.Functions.Like(author.Name, $"%{model.Name}%"))
                         .ToListAsync();

            int countElement = result.Count();

            result = result.Skip((pagination.PageNumber - Constants.Page.PAGE_NUMBER) * pagination.PageSize).Take(pagination.PageSize).ToList();

            var tupleResult = (result, countElement);

            return(tupleResult);
        }
Example #10
0
        public async Task <AuthorModel> GetAllAsync(AuthorFilterModel authorFilterModel)
        {
            var authorModel = new AuthorModel();
            var filterModel = authorFilterModel.MapToEFFilterModel();

            var listOfAuthors = await _authorRepository.GetAllAuthors(filterModel);

            if (listOfAuthors.Items == null)
            {
                authorModel.Errors.Add(Constants.Errors.NotFoundAuthorsError);
                return(authorModel);
            }

            authorModel.Counter = listOfAuthors.Counter;
            foreach (var author in listOfAuthors.Items)
            {
                var authorModelItem = new AuthorModelItem();
                authorModel.Items.Add(author.MapToModel());
            }

            return(authorModel);
        }
Example #11
0
        public async Task <PagedResponse <AuthorModel> > GetFilteredAuthorsAsync(AuthorFilterModel model, PaginationFilterModel pagination)
        {
            if (string.IsNullOrWhiteSpace(model.SortBy) && string.IsNullOrWhiteSpace(model.TypeSort))
            {
                model.SortBy   = Constants.Sort.DefaultSortById;
                model.TypeSort = Constants.Sort.DefaultSortByAsc;
            }

            (IEnumerable <Author> authors, int count)tupleAuthors = await _authorRepository.GetFilteredAuthorsAsync(model, pagination);

            var authorModel = _autoMapper.Map <IEnumerable <AuthorModel> >(tupleAuthors.authors);

            var pagedResponse = new PagedResponse <AuthorModel>
            {
                Data       = authorModel,
                PageNumber = pagination.PageNumber,
                PageSize   = pagination.PageSize,
                TotalItems = tupleAuthors.count
            };

            return(pagedResponse);
        }
Example #12
0
        public async Task <(IEnumerable <Author>, int)> GetFilteredAuthorsAsync(AuthorFilterModel model, PaginationFilterModel pagination)
        {
            using (var connection = CreateConnection())
            {
                var sql = new StringBuilder();
                sql.Append("SELECT author.Id, author.Name, pe.*");
                sql.Append($" FROM Authors as author");
                sql.Append($" INNER JOIN AuthorInPrintingEditions as auInpe on author.Id = auInpe.AuthorId");
                sql.Append($" INNER JOIN PrintingEditions as pe on pe.Id = auInpe.PrintingEditionId");
                sql.Append($" WHERE ((@Name = '' OR @Name = null) OR Name LIKE @Name )");

                var authors = await connection.QueryAsync <Author, PrintingEdition, Author>(sql.ToString(),
                                                                                            (author, printingEdition) =>
                {
                    author.AuthorInPrintingEdition.Add(new AuthorInPrintingEdition {
                        PrintingEdition = printingEdition
                    });
                    return(author);
                },
                                                                                            new { Name = $"%{model.Name}%" },
                                                                                            splitOn : "Id");


                var groupedAuthors = authors.GroupBy(author => author.Id).Select(groupAuthor =>
                {
                    var groupedAuthor = groupAuthor.First();
                    groupedAuthor.AuthorInPrintingEdition = groupAuthor.Select(author => author.AuthorInPrintingEdition.FirstOrDefault()).ToList();
                    return(groupedAuthor);
                });

                int countElement = authors.Count();

                groupedAuthors = groupedAuthors.Skip((pagination.PageNumber - Constants.Page.PAGE_NUMBER) * pagination.PageSize).Take(pagination.PageSize).ToList();

                (IEnumerable <Author>, int)tupleResult = (groupedAuthors, countElement);

                return(tupleResult);
            }
        }
Example #13
0
        public void UpdatePrintingEdition(PrintingEditionModel printingEdition)
        {
            _validator.ValidatePrintingEdition(printingEdition);
            var dbPrintingEdition = _printingEditionRepository.GetById(printingEdition.Id);

            if (dbPrintingEdition is null)
            {
                throw new CustomApiException(HttpStatusCode.BadRequest, Constants.PRINTINGEDITIONNOTFOUNDERROR);
            }
            _mapper.Map(printingEdition, dbPrintingEdition);
            var authorFilter = new AuthorFilterModel {
                EditionAuthors = printingEdition.Authors
            };
            var authors = _authorRepository.GetAll(authorFilter);

            dbPrintingEdition.Authors.Clear();
            var dbAuthors = dbPrintingEdition.Authors.ToList();

            dbAuthors.AddRange(authors);
            dbPrintingEdition.Authors = dbAuthors;
            _printingEditionRepository.Update(dbPrintingEdition);
        }
Example #14
0
        public async Task <IActionResult> GetAll([FromBody] AuthorFilterModel authorFilter)
        {
            var authorModel = await _authorService.GetAllAsync(authorFilter);

            return(Ok(authorModel));
        }