Exemplo n.º 1
0
        public Task CreateAsync(AssetCategory entity)
        {
            _dbContext.AssetCategories.Add(entity);

            _dbContext.SaveChangesAsync();

            return Task.FromResult(0);
        }
Exemplo n.º 2
0
        public Task UpdateAsync(AssetCategory entity)
        {
            foreach(var existingField in _dbContext.AssetFields)
            {
                var current = entity.AssetFields.FirstOrDefault(a => a.Id == existingField.Id);

                if(current == null)
                {
                    _dbContext.Entry<AssetField>(existingField).State = EntityState.Deleted;
                }
                else
                {
                    _dbContext.AssetFields.Remove(existingField);
                }
            }

            //var newAssetFields = entity.AssetFields.Where(a => a.Id <= 0);

            //foreach(var newAssetField in newAssetFields)
            //{
            //    entity.AssetFields.Remove(newAssetField);
            //}

            var assetFields = entity.AssetFields;
            entity.AssetFields = new Collection<AssetField>();

            foreach (var assetField in assetFields)
            {
                if (assetField.Id <= 0)
                {
                    entity.AssetFields.Add(assetField);
                }
                else
                {
                    _dbContext.Entry<AssetField>(assetField).State = EntityState.Modified;
                }
            }

            _dbContext.Entry<AssetCategory>(entity).State = EntityState.Modified;

            //foreach(var assetField in assetFields)
            //{
            //    entity.AssetFields.Add(assetField);
            //}

            //foreach (var newAssetField in newAssetFields)
            //{
            //    entity.AssetFields.Add(newAssetField);
            //}

            _dbContext.SaveChangesAsync();

            return Task.FromResult(0);
        }
Exemplo n.º 3
0
        public async Task<IHttpActionResult> Create([ModelBinder(typeof(FieldValueModelBinder))] CreateAssetCategoryModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var entity = new AssetCategory
            {
                Name = model.Name,
                Description = model.Description
            };

            foreach (var assetFieldModel in model.AssetFields)
            {
                if (assetFieldModel != null)
                {
                    Validate(assetFieldModel);

                    if(!ModelState.IsValid)
                    {
                        return BadRequest(ModelState);
                    }

                    var assetField = new AssetField
                    {
                        FieldType = assetFieldModel.FieldType,
                        Name = assetFieldModel.Name,
                        Description = assetFieldModel.Description,
                        Position = assetFieldModel.Position,
                        ValidationRules = assetFieldModel.ValidationRules,
                    };

                    entity.AssetFields.Add(assetField);
                }
            }

            var response = await _manager.CreateAsync(entity);

            if (!response.IsSuccessful)
            {
                ModelState.AddModelError("", response.Error.Message);

                return BadRequest(ModelState);
            }

            return Ok(entity.ToPresenter());
        }
Exemplo n.º 4
0
        public async Task<IResponse<bool>> CreateAsync(AssetCategory entity)
        {
            var response = new Response<bool>();

            try
            {
                await _store.CreateAsync(entity);

                response.Result = true;
            }catch(Exception ex)
            {
                response.Error = new Error(ex.Message);
            }

            return response;
        }