Beispiel #1
0
        public virtual ListEntityResponse <TModel> Create(IEnumerable <TModel> models)
        {
            var responses = new List <SingleEntityResponse <TModel> >();

            foreach (var model in models)
            {
                if (model.Id == null || model.Id == Guid.Empty)
                {
                    model.Id = Guid.NewGuid();
                }
                var data             = Mapper.Map(model, Activator.CreateInstance <TEntity>());
                var validationResult = new CommonValidator <TModel>(this, _translationsService).Validate(model);
                if (!validationResult.IsValid)
                {
                    responses.Add(new SingleEntityResponse <TModel>(model, validationResult.GetErrorsObjects().ToList()));
                }
                else
                {
                    responses.Add(new SingleEntityResponse <TModel>(Mapper.Map(_repository.Add(data), model)));
                }
            }

            if (responses.Any(r => r.Errors != null))
            {
                return(new ListEntityResponse <TModel>(responses));
            }

            _unitOfWork.SaveChanges();
            return(new ListEntityResponse <TModel>(responses));
        }
Beispiel #2
0
        public virtual SingleEntityResponse <TModel> Create(TModel model)
        {
            if (model.Id == null || model.Id == Guid.Empty)
            {
                model.Id = Guid.NewGuid();
            }
            var data = Mapper.Map(model, Activator.CreateInstance <TEntity>());

            var validationResult = new CommonValidator <TModel>(this, _translationsService).Validate(model);

            if (!validationResult.IsValid)
            {
                return(new SingleEntityResponse <TModel>(model, validationResult.GetErrorsObjects().ToList()));
            }

            var entity = _repository.Add(data);

            _unitOfWork.SaveChanges();
            return(new SingleEntityResponse <TModel>(Mapper.Map(entity, model)));
        }