diff --git a/Backend/Backend/Controllers/IssuesController.cs b/Backend/Backend/Controllers/IssuesController.cs index c22b831..64c38d0 100644 --- a/Backend/Backend/Controllers/IssuesController.cs +++ b/Backend/Backend/Controllers/IssuesController.cs @@ -108,39 +108,39 @@ public async Task SubmitQuest(long id) quest.CompletedAt = DateTime.UtcNow; var user = await _context.Users.FindAsync(userId); - if (user != null) - { - user.ExperiencePoints += issue.XPReward; + if (user == null) + return NotFound("User account not found."); + + user.ExperiencePoints += issue.XPReward; - // Update streak based on contribution dates (once per day) - var today = DateTime.UtcNow.Date; - if (user.LastContributionDate.HasValue) + // Update streak based on contribution dates (once per day) + var today = DateTime.UtcNow.Date; + if (user.LastContributionDate.HasValue) + { + var lastDate = user.LastContributionDate.Value.Date; + if (lastDate == today) + { + // Already contributed today – do not increment streak + } + else if (lastDate == today.AddDays(-1)) { - var lastDate = user.LastContributionDate.Value.Date; - if (lastDate == today) - { - // Already contributed today – do not increment streak - } - else if (lastDate == today.AddDays(-1)) - { - // Contributed yesterday – extend streak - user.CurrentStreak += 1; - } - else - { - // Gap in contributions – reset streak - user.CurrentStreak = 1; - } + // Contributed yesterday – extend streak + user.CurrentStreak += 1; } else { + // Gap in contributions – reset streak user.CurrentStreak = 1; } - - user.LastContributionDate = DateTime.UtcNow; } + else + { + user.CurrentStreak = 1; + } + + user.LastContributionDate = DateTime.UtcNow; await _context.SaveChangesAsync(); - return Ok(new { message = $"{issue.XPReward} XP Awarded!", totalXp = user?.ExperiencePoints }); + return Ok(new { message = $"{issue.XPReward} XP Awarded!", totalXp = user.ExperiencePoints }); } } \ No newline at end of file diff --git a/Backend/Backend/Migrations/20260315082912_InitialCreate.Designer.cs b/Backend/Backend/Migrations/20260315082912_InitialCreate.Designer.cs index 503f37e..5d41354 100644 --- a/Backend/Backend/Migrations/20260315082912_InitialCreate.Designer.cs +++ b/Backend/Backend/Migrations/20260315082912_InitialCreate.Designer.cs @@ -125,7 +125,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.Property("GitHubId") .IsRequired() - .HasColumnType("nvarchar(max)"); + .HasColumnType("nvarchar(450)"); b.Property("GitHubUsername") .IsRequired() @@ -136,6 +136,9 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasKey("Id"); + b.HasIndex("GitHubId") + .IsUnique(); + b.HasIndex("GitHubUsername") .IsUnique(); diff --git a/Backend/Backend/Migrations/20260315082912_InitialCreate.cs b/Backend/Backend/Migrations/20260315082912_InitialCreate.cs index e6e829f..9b406ba 100644 --- a/Backend/Backend/Migrations/20260315082912_InitialCreate.cs +++ b/Backend/Backend/Migrations/20260315082912_InitialCreate.cs @@ -55,7 +55,7 @@ protected override void Up(MigrationBuilder migrationBuilder) columns: table => new { Id = table.Column(type: "uniqueidentifier", nullable: false), - GitHubId = table.Column(type: "nvarchar(max)", nullable: false), + GitHubId = table.Column(type: "nvarchar(450)", nullable: false), GitHubUsername = table.Column(type: "nvarchar(450)", nullable: false), Email = table.Column(type: "nvarchar(max)", nullable: true), AvatarUrl = table.Column(type: "nvarchar(max)", nullable: true), @@ -111,6 +111,12 @@ protected override void Up(MigrationBuilder migrationBuilder) column: "GitHubIssueId", unique: true); + migrationBuilder.CreateIndex( + name: "IX_Users_GitHubId", + table: "Users", + column: "GitHubId", + unique: true); + migrationBuilder.CreateIndex( name: "IX_Users_GitHubUsername", table: "Users", diff --git a/Backend/Backend/Migrations/20260315125659_AddUniqueConstraints.cs b/Backend/Backend/Migrations/20260315125659_AddUniqueConstraints.cs index 4f15c30..687b757 100644 --- a/Backend/Backend/Migrations/20260315125659_AddUniqueConstraints.cs +++ b/Backend/Backend/Migrations/20260315125659_AddUniqueConstraints.cs @@ -14,14 +14,6 @@ protected override void Up(MigrationBuilder migrationBuilder) name: "IX_Quests_UserId", table: "Quests"); - migrationBuilder.AlterColumn( - name: "GitHubId", - table: "Users", - type: "nvarchar(450)", - nullable: false, - oldClrType: typeof(string), - oldType: "nvarchar(max)"); - migrationBuilder.AlterColumn( name: "Status", table: "Quests", @@ -30,12 +22,6 @@ protected override void Up(MigrationBuilder migrationBuilder) oldClrType: typeof(string), oldType: "nvarchar(max)"); - migrationBuilder.CreateIndex( - name: "IX_Users_GitHubId", - table: "Users", - column: "GitHubId", - unique: true); - migrationBuilder.CreateIndex( name: "IX_Quests_UserId_GitHubIssueId_ActiveStatus", table: "Quests", @@ -47,22 +33,10 @@ protected override void Up(MigrationBuilder migrationBuilder) /// protected override void Down(MigrationBuilder migrationBuilder) { - migrationBuilder.DropIndex( - name: "IX_Users_GitHubId", - table: "Users"); - migrationBuilder.DropIndex( name: "IX_Quests_UserId_GitHubIssueId_ActiveStatus", table: "Quests"); - migrationBuilder.AlterColumn( - name: "GitHubId", - table: "Users", - type: "nvarchar(max)", - nullable: false, - oldClrType: typeof(string), - oldType: "nvarchar(450)"); - migrationBuilder.AlterColumn( name: "Status", table: "Quests", diff --git a/Backend/Backend/Program.cs b/Backend/Backend/Program.cs index cb3098c..28761fa 100644 --- a/Backend/Backend/Program.cs +++ b/Backend/Backend/Program.cs @@ -52,7 +52,7 @@ var secretKey = jwtSettings["Key"]; if (string.IsNullOrEmpty(secretKey)) throw new InvalidOperationException("JwtSettings:Key is not configured. Set it via environment variable or user secrets."); -var key = Encoding.ASCII.GetBytes(secretKey); +var key = Encoding.UTF8.GetBytes(secretKey); builder.Services.AddAuthentication(options => {