Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateUserApplyAsync(string id, Dictionary <int, string> data)
        {
            try
            {
                var user = await _userManager.FindByIdAsync(id);

                // 确保用户存在有效申请
                if (user.Application == null)
                {
                    user.Application = new Application
                    {
                        User         = user,
                        PeriodId     = 1,
                        IsSuccessful = null
                    };
                }
                // 校验申请信息修改合法性
                var dataTypes = await _dbContext.ApplicationPeriodDataType.ToDictionaryAsync(e => e.Id);

                foreach (var key in data.Keys)
                {
                    if (!dataTypes.ContainsKey(key))
                    {
                        throw new Exception("指定的申请信息类型无效");
                    }
                }
                // 匹配数据库已有的信息并修改
                var existingData = await _dbContext.ApplicationPeriodData
                                   .Where(d => d.ApplicationId == user.Id)
                                   .ToDictionaryAsync(d => d.DataTypeId);

                var newData = new List <ApplicationPeriodData>();
                foreach (var(key, value) in data)
                {
                    if (existingData.ContainsKey(key))
                    {
                        existingData[key].Value = value ?? string.Empty;
                    }
                    else
                    {
                        newData.Add(new ApplicationPeriodData
                        {
                            ApplicationId = user.Id,
                            DataTypeId    = key,
                            Value         = value ?? string.Empty
                        });
                    }
                }
                _dbContext.AddRange(newData);
                await _dbContext.SaveChangesAsync();

                return(Json(new { succeeded = true }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新用户信息时发生错误");
                return(Json(new { succeeded = false, message = ex.Message }));
            }
        }
Ejemplo n.º 2
0
        public async Task <int> CreateProblemAsync(Problem problem)
        {
            await _dbContext.Problem.AddAsync(problem);

            await _dbContext.SaveChangesAsync();

            return(problem.Id);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> UpdateAsync(Dictionary <int, string> data)
        {
            try
            {
                var userId       = _userManager.GetUserId(User);
                var existingData = await _dbContext.UserData
                                   .Where(d => d.UserId == userId)
                                   .ToDictionaryAsync(d => d.DataTypeId);

                var newData = new List <UserData>();
                foreach (var kv in data)
                {
                    if (existingData.ContainsKey(kv.Key))
                    {
                        existingData[kv.Key].Value = kv.Value ?? string.Empty;
                    }
                    else
                    {
                        newData.Add(new UserData
                        {
                            UserId     = userId,
                            DataTypeId = kv.Key,
                            Value      = kv.Value ?? string.Empty
                        });
                    }
                }
                _dbContext.AddRange(newData);
                await _dbContext.SaveChangesAsync();

                return(Json(new { succeeded = true }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新用户信息时发生错误");
                return(Json(new { succeeded = false, message = ex.Message }));
            }
        }