public CreateEntityRequestHandler(IContext context)
 {
     _context      = context;
     _innerHandler = new EntityRequestHandler <TEntity, TEntity>(async req =>
     {
         var result = await _context.AddEntity <TEntity>(req.Request);
         await _context.SaveAsync();
         return(result);
     });
 }
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)
            });
        }
Ejemplo n.º 3
0
 public virtual void AddEntity(TEntity entity)
 {
     _context.AddEntity <TEntity>(entity);
 }