コード例 #1
0
        public async Task <IActionResult> UpdateMainSettings([FromBody] MainSettings settings)
        {
            string accessToken = await HttpContext.GetToken();

            var session = await sessionService.GetSession(accessToken);

            if (session == null)
            {
                return(Unauthorized(new { message = "Session expired. Please login again." }));
            }
            try
            {
                if (settings == null)
                {
                    throw new ArgumentNullException("Settings cannot be null");
                }
                if (settings.UserId != session.UserId)
                {
                    throw new NotSupportedException("You are not allowed to change other user's settings");
                }

                var existing = (await userRepository.GetSettings(session.UserId)).ToDto <UserSettings>();
                existing.MainSettings = settings.ToJson();
                var updated = await userRepository.UpdateSettings(existing);

                await log.InfoAsync("User Settings updated", context : session.UserId);

                return(Ok(settings));
            }
            catch (Exception ex)
            {
                await log.ErrorAsync("Error in userRepository.GetSettings()", ex);

                return(BadRequest(new { title = ex.GetType().ToString(), details = ex.StackTrace, message = ex.Message }));
            }
        }