public ServicesController(UserContext context)
        {
            _context = context;

            if (_context.Services.Count() == 0)
            {
                apiHelper = new ServiceApi();
                _context.AddRangeAsync(apiHelper.GetServiceData());
                _context.SaveChangesAsync();
            }
        }
예제 #2
0
        public async Task <IActionResult> Patch([FormBody] JsonPatchDocument <AppUser> patch)
        {
            var user = await _dbContext.AppUsers
                       .SingleOrDefaultAsync(x => x.Id == UserIdentity.UserId);

            if (user == null)
            {
                return(BadRequest());
            }
            //将需要更新的数据复制给对象
            patch.ApplyTo(user);
            //不使用 EF 进行追踪appUser实体的properties属性
            foreach (var item in user?.Properties)
            {
                _dbContext.Entry(item).State = EntityState.Detached;
            }
            var currentPro = user.Properties;
            var originPros = await _dbContext.AppUserProperties
                             .AsNoTracking().Where(b => b.AppUserId == UserIdentity.UserId).ToListAsync();

            var allPros    = originPros.Union(currentPro).Distinct();
            var addRang    = allPros.Except(originPros);
            var removeRang = originPros.Except(currentPro);

            _dbContext.Remove(removeRang);

            using (var trans = await _dbContext.Database.BeginTransactionAsync())
            {
                await _dbContext.AddRangeAsync(user);

                await _dbContext.SaveChangesAsync();

                trans.Commit();
            }
            return(Ok(user));
        }
예제 #3
0
        public async Task <IActionResult> UpdateUserTags([FromBody] List <string> tags)
        {
            var currentTags = await _userContext.UserTags.Where(s => s.UserId.Equals(UserIdentity.UserId)).ToListAsync();

            var newTags = tags.Except(currentTags.Select(s => s.Tag));
            await _userContext.AddRangeAsync(newTags.Select(s => new UserTag
            {
                CreateTime = DateTime.Now,
                UserId     = UserIdentity.UserId,
                Tag        = s
            }));

            await _userContext.SaveChangesAsync();

            return(Json("success! updatedate:" + DateTime.Now));
        }
예제 #4
0
        public async Task <IActionResult> UpdateUserTags(List <string> tags)
        {
            var userTags = await _userContext.UserTag.Where(u => u.UserId == UserIdentity.UserId).ToListAsync();

            //Except比较两个序列的每一个值,然后得到不到的值的集合
            var newTags = tags.Except(userTags.Select(t => t.Tag));
            //使用AddRangeAsync一次插入多条数据
            await _userContext.AddRangeAsync(newTags.Select(tag => new UserTag
            {
                UserId = UserIdentity.UserId,
                Tag    = tag
            }));

            await _userContext.SaveChangesAsync();

            return(Ok());
        }