Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #28 from brminnick/Add-IAdvocateApi
Browse files Browse the repository at this point in the history
Fix OptOutDatabase.PatchOptOutModel
  • Loading branch information
brminnick authored Jul 5, 2021
2 parents 683e411 + 6bc5437 commit 2d3e2f0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
10 changes: 10 additions & 0 deletions GitHubReadmeWebTrends.Common/Database/OptOutDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -37,10 +38,19 @@ public async Task<OptOutModel> PatchOptOutModel(OptOutModel optOutModel)
{
optOutModel = optOutModel with { UpdatedAt = DateTimeOffset.UtcNow };

if (isEntityTacked(optOutModel, out var trackedOptOutModel))
_dbContext.Entry(trackedOptOutModel).State = EntityState.Detached;

var entityEntry = _dbContext.Update(optOutModel);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);

return entityEntry.Entity;

bool isEntityTacked(OptOutModel optOutModel, out OptOutModel? trackedOptOutModel)
{
trackedOptOutModel = _dbContext.OptOutDatabaseModel.Local.FirstOrDefault(entry => entry.Alias.Equals(optOutModel.Alias));
return trackedOptOutModel != null;
}
}

internal async Task<OptOutModel> RemoveOptOutModel(string id)
Expand Down
2 changes: 1 addition & 1 deletion GitHubReadmeWebTrends.Common/Database/OptOutDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public OptOutDbContext(DbContextOptions<OptOutDbContext> options) : base(options
Database.EnsureCreated();
}

public DbSet<OptOutModel>? OptOutDatabaseModel => Set<OptOutModel>();
public DbSet<OptOutModel> OptOutDatabaseModel => Set<OptOutModel>();
}
}
2 changes: 1 addition & 1 deletion GitHubReadmeWebTrends.Website/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public async Task OnPostOptOutButtonClicked()
{
_logger.LogInformation("Opt Out Button Clicked");

var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));

var microsoftAlias = await GetCurrentUserAlias().ConfigureAwait(false);

var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var currentAdvocates = await _advocateService.GetCurrentAdvocates(cancellationTokenSource.Token).ConfigureAwait(false);
var matchingAdvocate = currentAdvocates.SingleOrDefault(x => x.MicrosoftAlias == microsoftAlias);

Expand Down
2 changes: 1 addition & 1 deletion GitHubReadmeWebTrends.Website/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Startup
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
string[] initialScopes = Configuration.GetValue<string>("DownstreamApi:Scopes")?.Split(' ') ?? Array.Empty<string>();
var initialScopes = Configuration.GetValue<string>("DownstreamApi:Scopes")?.Split(' ') ?? Array.Empty<string>();

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"))
Expand Down

0 comments on commit 2d3e2f0

Please sign in to comment.