Пример #1
0
        public async Task <Item> UpdateItemAsync(UpsertItemParams ps)
        {
            _cache.Remove($"item-{ps.ItemKey}");

            var oldItem = await _context.Items.FirstOrDefaultAsync(i => i.ItemKey == ps.ItemKey);

            if (oldItem == null)
            {
                throw new NotFoundException($"Item {ps.ItemKey} cannot be found.");
            }

            // verification
            if (!ps.ItemKey.Split(".")[0].Equals(ps.ProjectKey, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidParamsException("The item key should be started with it's project key.");
            }

            _mapper.Map(ps, oldItem);
            // make the list be tracked
            oldItem.ParameterInfos = ps.ParameterInfos;
            oldItem.Rules          = ps.Rules;

            await _context.SaveChangesAsync();

            return(oldItem);
        }
Пример #2
0
        public async Task <Item> AddItemAsync(UpsertItemParams ps)
        {
            if (await _context.Items.AsNoTracking().AnyAsync(i => i.ItemKey == ps.ItemKey))
            {
                throw new ConflictException($"Item with key {ps.ItemKey} is already existing.");
            }

            // verification
            if (!ps.ItemKey.Split(".")[0].Equals(ps.ProjectKey, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidParamsException("The item key should be started with it's project key.");
            }

            var item = _mapper.Map <Item>(ps);

            await _context.Items.AddAsync(item);

            await _context.SaveChangesAsync();

            return(item);
        }