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); }
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); }
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); }
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); }
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); }
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()); }
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); }
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); }
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); }
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); }
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); } }
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); }
public async Task <IActionResult> GetAll([FromBody] AuthorFilterModel authorFilter) { var authorModel = await _authorService.GetAllAsync(authorFilter); return(Ok(authorModel)); }