コード例 #1
0
        public async Task <RecordResult> SignOut([FromBody] SignOutModel model)
        {
            // 从数据库获取签到记录
            Record record = await(from r in _context.Records
                                  where r.SignOutTime == null &&
                                  r.StudentId == model.StudentId
                                  select r).FirstAsync();

            // 签退
            record.SignOut();

            // 更新签到记录
            try
            {
                _context.Update(record);
                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                _logger.LogError($"DataBase Error: {e.Message}");
                return(RecordResult.Fail(RecordErrorResource.DataBaseError));
            }

            return(RecordResult.Success());
        }
コード例 #2
0
        public async Task OnActionExecutionAsync(
            ActionExecutingContext context,
            ActionExecutionDelegate next)
        {
            if (!context.ModelState.IsValid)
            {
                List <string> errorMsgs = new List <string>();
                foreach (var item in context.ModelState.Values)
                {
                    errorMsgs.AddRange(item.Errors.Select(error => error.ErrorMessage));
                }

                context.Result = new JsonResult(RecordResult.Fail(errorMsgs.Distinct().ToArray()));
            }
            else
            {
                var resultContext = await next();
            }
        }
コード例 #3
0
        public async Task <RecordResult> SignIn([FromBody] SignInModel model)
        {
            // 创建签到记录
            Record record = model.Create();

            // 签到
            record.SignIn();

            // 将记录添加到数据库
            try
            {
                _context.Add(record);
                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                _logger.LogError($"DataBase Error: {e.Message}");
                return(RecordResult.Fail(RecordErrorResource.DataBaseError));
            }


            return(RecordResult.Success());
        }