public async Task <PagedApiResponse <IEnumerable <ReadDocumentListResponse> > > Handle(GetDocumentsByTermsQuery request, CancellationToken cancellationToken) { // validate page request.ValidateValues(); // check if type exist if (request.Type != null && await _documentTypeRepository.FindByIdAsync(request.Type.Value) == null) { throw new ApiProblemDetailsException($"Type with id: {request.Type} does not exist.", StatusCodes.Status404NotFound); } // check if tag exist DocumentTag tag = null; if (!string.IsNullOrWhiteSpace(request.Tag)) { tag = await _documentTagRepository.FindBySlugAsync(_authenticatedUser.UserId, request.Tag); if (tag == null) { throw new ApiProblemDetailsException($"Tag with name: {request.Tag} does not exist.", StatusCodes.Status404NotFound); } } // make request PaginatedList <Document> paginatedList = await _repository.GetPagedReponseAsync(_authenticatedUser.UserId, request.Page, request.PageSize, request.Term, request.Type, tag?.Id); IEnumerable <ReadDocumentListResponse> dto = _mapper.Map <IEnumerable <ReadDocumentListResponse> >(paginatedList.Items); ApiResponsePagination pagination = ApiResponsePagination.Build(paginatedList); return(new PagedApiResponse <IEnumerable <ReadDocumentListResponse> >(dto, pagination)); }
public async Task <BasicApiResponse <ReadDocumentResponse> > Handle(CreateDocumentCommand request, CancellationToken cancellationToken) { // data Document document = _mapper.Map <Document>(request); DocumentFile file = await BuildFromFileAsync(request.File); document.File = file; document.TypeId = request.Type; // tags IEnumerable <string> validTags = request.Tags.Where(tag => !string.IsNullOrWhiteSpace(tag)); if (validTags != null && validTags.Count() > 0) { IEnumerable <DocumentTag> existingTags = await _documentTagRepository.GetSameUniqueNameAsync(_authenticatedUser.UserId, validTags.Select(tag => Slugger.Generate(tag))); // add already exiting tags _documentTagRepository.AttachRange(existingTags); foreach (DocumentTag documentTag in existingTags) { document.Tags.Add(documentTag); } // add new tags IEnumerable <string> existingTagSlugs = existingTags.Select(t => t.Slug); foreach (string tag in validTags) { string slug = Slugger.Generate(tag); if (!existingTagSlugs.Contains(slug)) { document.Tags.Add(new DocumentTag { Name = tag, Slug = slug }); } } } // insert in database await _documentRepository.AddAsync(document); // fill type if (document.TypeId != null) { document.Type = await _documentTypeRepository.FindByIdAsync(document.TypeId.Value); } ReadDocumentResponse dto = _mapper.Map <ReadDocumentResponse>(document); return(new BasicApiResponse <ReadDocumentResponse>(dto)); }
public async Task <BasicApiResponse <ReadDocumentTypeResponse> > Handle(GetDocumentTypeByIdQuery query, CancellationToken cancellationToken) { DocumentType type = await _repository.FindByIdAsync(query.Id); if (type == null) { throw new ApiProblemDetailsException($"Record with id: {query.Id} does not exist.", StatusCodes.Status404NotFound); } ReadDocumentTypeResponse dto = _mapper.Map <ReadDocumentTypeResponse>(type); return(new BasicApiResponse <ReadDocumentTypeResponse>(dto)); }
public async Task <DocumentType> GetDocumentTypeAsync(int id) => documentType ??= await _documentTypeRepository.FindByIdAsync(id);