Ejemplo n.º 1
0
        /// <summary>
        /// Singe function that takes a list of search filters and returns a list of formatted models
        /// </summary>
        /// <param name="searchFilters">The search filters we wish to filter by</param>
        /// <returns>A list of formatted models</returns>
        public async Task <List <TModel> > GetSearchResults(List <SearchFilterModel> searchFilters)
        {
            // Get search results for our entities
            var searchResults = await _context.GetEntities <TEntity>(searchFilters);

            var results = new List <TModel>();

            // Format them into models
            foreach (var result in searchResults)
            {
                // Create a new model using the mapper
                var newModel = _mapper.CreateModel(result);

                // If we have a search formatter we can use it here to populate any additional data
                if (_searchFormatter != null)
                {
                    newModel = await _searchFormatter?.FormatModel(newModel);
                }

                // Add to the list
                results.Add(newModel);
            }

            return(results);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add an entry to the context
        /// </summary>
        /// <param name="model">The model to add</param>
        /// <returns>A response model containing any validation results, and the new model if successfully created</returns>
        public async Task <RepositoryActionResultModel <TModel> > Add(TModel model)
        {
            // Could add some validation to see if the user is allowed to create this type of entity
            //  as part of a rule based system...?

            // Validate the incoming model against the registered validator
            var validationResult = await _validator.ValidateAdd(model);

            // Return a new result model if validation has failed
            if (!validationResult.IsValid)
            {
                return(new RepositoryActionResultModel <TModel>
                {
                    CurrentVersion = model,
                    ResultType = ResultTypeEnum.FailedValidation,
                    ValidationResult = validationResult
                });
            }

            // If passed, assume all ok and create a new entity
            var newEntity = _mapper.CreateEntity <TEntity>(model);

            // Assign a few defaults, guid and who created it
            newEntity.Id              = Guid.NewGuid();
            newEntity.DateCreated     = DateTime.UtcNow;
            newEntity.CreatedByUserId = _authenticatedUser.UserId;
            newEntity.EntityStatus    = EntityStatusEnum.Active;

            // Add the entity
            await _context.AddEntity(newEntity);

            // Save to the db, this could be done in a
            //  unit of work pattern and saved later
            await _context.SaveChangesAsync();

            // Create a result containing old and new version, and return
            return(new RepositoryActionResultModel <TModel>
            {
                ResultType = ResultTypeEnum.Success,
                PreviousVersion = null,
                CurrentVersion = _mapper.CreateModel(newEntity)
            });
        }