Skip to content

Commit

Permalink
Fixed new user settings record creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mattahearn committed Apr 22, 2024
1 parent 78dcd6c commit 7fe9086
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions webapi/Controllers/UserSettingsController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using CopilotChat.WebApi.Hubs;
using CopilotChat.WebApi.Models.Request;
Expand Down Expand Up @@ -45,17 +47,35 @@ public UserSettingsController(
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetUserSettingsAsync(Guid userId)
{
var settings = await this._userSettingsRepository.FindSettingsByUserIdAsync(userId.ToString());
IEnumerable<UserSettings> settings;
try
{
try
{
settings = await this._userSettingsRepository.FindSettingsByUserIdAsync(userId.ToString());
}
catch (Exception)
{
// No record found, create a new settings record for this user
var newUserSettings = new UserSettings(userId.ToString(), false, false, false, true, true, false, false, false, true, true, false);
await this._userSettingsRepository.CreateAsync(newUserSettings);
return this.Ok(newUserSettings); // Only 1 record per user id
}

foreach (var setting in settings)
foreach (var setting in settings)
{
UserSettings us = new(setting.UserId, setting.DarkMode, setting.Planners, setting.Personas, setting.SimplifiedChatExperience,
setting.AzureContentSafety, setting.AzureAISearch, setting.ExportChatSessions, setting.LiveChatSessionSharing,
setting.FeedbackFromUser, setting.DeploymentGPT35, setting.DeploymentGPT4);
return this.Ok(us); // Only 1 record per user id
}
}
catch (Exception ex)
{
UserSettings us = new(setting.UserId, setting.DarkMode, setting.Planners, setting.Personas, setting.SimplifiedChatExperience,
setting.AzureContentSafety, setting.AzureAISearch, setting.ExportChatSessions, setting.LiveChatSessionSharing,
setting.FeedbackFromUser, setting.DeploymentGPT35, setting.DeploymentGPT4);
return this.Ok(us); // Only 1 record per user id
this._logger.LogError(ex.ToString());
}

return this.NotFound("Did not find any user specific settings.");
return this.NotFound(" Did not find any user specific settings.");
}

/// <summary>
Expand Down Expand Up @@ -102,6 +122,6 @@ public async Task<IActionResult> UpdateUserSettingsAsync(
return this.Ok(newUserSettings);
}

return this.NotFound("User ID was not sent to update user settings'.");
return this.NotFound(" User ID was not sent to update user settings'.");
}
}

0 comments on commit 7fe9086

Please sign in to comment.