Пример #1
0
        public async Task <ActionResult <WebData> > GetAsync()
        {
            var userId = HttpContext.GetUserId();

            try
            {
                var user = await _db.Users.AsNoTracking().Include(u => u.WebData).FirstOrDefaultAsync(u => u.Id == userId);

                if (user == null)
                {
                    return(Forbid(AuthHandler.SchemeName));
                }

                _actions.Labels("get").Inc();

                return(Ok(WebData.FromDbModel(user.WebData)));
            }
            catch (Exception e)
            {
                var message = $"Could not retrieve sync data for user {userId}.";

                _logger.LogWarning(e, message);

                return(StatusCode(500, message));
            }
        }
Пример #2
0
        public async Task <ActionResult> PatchAsync(SyncRequest request)
        {
            var userId = HttpContext.GetUserId();

            try
            {
                var user = await _db.Users.Include(u => u.WebData).FirstOrDefaultAsync(u => u.Id == userId);

                if (user == null)
                {
                    return(Forbid(AuthHandler.SchemeName));
                }

                var data = user.WebData;

                if (data.Token != request.Token)
                {
                    return(BadRequest(WebData.FromDbModel(data)));
                }

                try
                {
                    var current = JObject.Parse(data.Data);

                    request.Patch.ApplyTo(current);

                    data.Data = current.ToString(Formatting.None);
                }
                catch
                {
                    return(BadRequest(WebData.FromDbModel(data)));
                }

                data.Token = Guid.NewGuid();

                await _db.SaveChangesAsync();

                _actions.Labels("patch").Inc();

                return(Ok(new SyncResponse
                {
                    Token = data.Token
                }));
            }
            catch (Exception e)
            {
                var message = $"Could not patch sync data for user {userId}.";

                _logger.LogWarning(e, message);

                return(StatusCode(500, message));
            }
        }