Skip to content

Commit

Permalink
Merge pull request #4 from zimotech10/feat/auth
Browse files Browse the repository at this point in the history
boiler plate completed
  • Loading branch information
zimotech10 authored Aug 29, 2024
2 parents bbc5357 + d945508 commit a3d8400
Show file tree
Hide file tree
Showing 17 changed files with 363 additions and 6 deletions.
28 changes: 26 additions & 2 deletions Extensions/AuthEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private static async Task<IResult> Register(

await userManager.AddToRoleAsync(user, role);

return Results.Ok("User registered successfully with role " + role);
// Return success message with the new user's ID
return Results.Ok(new { Message = "User registered successfully with role " + role, UserId = user.Id });
}

private static async Task<IResult> Login(
Expand Down Expand Up @@ -99,7 +100,9 @@ private static string GenerateJwtToken(Users user, IConfiguration configuration)
return new JwtSecurityTokenHandler().WriteToken(token);
}

private static async Task<IResult> UploadAvatar(IFormFile avatarFile)
private static async Task<IResult> UploadAvatar(IFormFile avatarFile, UserManager<Users> userManager,
ApplicationDbContext dbContext,
string userId) // Pass userId to identify the user
{
if (avatarFile == null || avatarFile.Length == 0)
{
Expand Down Expand Up @@ -131,6 +134,27 @@ private static async Task<IResult> UploadAvatar(IFormFile avatarFile)
await avatarFile.CopyToAsync(stream);
}

// Update user's avatar path in the database
var user = await userManager.FindByIdAsync(userId);
if (user == null)
{
return Results.NotFound("User not found.");
}

user.PhotoUrl = $"/uploads/{fileName}";

var result = await userManager.UpdateAsync(user);
if (!result.Succeeded) //If operation is failed delete uploaded file
{
// Handle update failure (rollback file creation, etc.)
File.Delete(filePath);
return Results.BadRequest(result.Errors);
}

// Save changes to the database
await dbContext.SaveChangesAsync();


return Results.Ok(new { FilePath = $"/uploads/{fileName}" });
}

Expand Down
301 changes: 301 additions & 0 deletions Migrations/20240829041317_AddPhotoUrlToUsers.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Migrations/20240829041317_AddPhotoUrlToUsers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace freelancerAPI.Migrations
{
/// <inheritdoc />
public partial class AddPhotoUrlToUsers : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "PhotoUrl",
table: "AspNetUsers",
type: "nvarchar(max)",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "PhotoUrl",
table: "AspNetUsers");
}
}
}
3 changes: 3 additions & 0 deletions Migrations/ApplicationDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<bool>("PhoneNumberConfirmed")
.HasColumnType("bit");

b.Property<string>("PhotoUrl")
.HasColumnType("nvarchar(max)");

b.Property<string>("SecurityStamp")
.HasColumnType("nvarchar(max)");

Expand Down
1 change: 1 addition & 0 deletions Models/Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public class Users : IdentityUser
public required string Country { get; set; }
public required string Bio { get; set; }
public required string BioTitle { get; set; }
public string? PhotoUrl { get; set; }
}
Binary file modified bin/Debug/net8.0/freelancerAPI.dll
Binary file not shown.
Binary file modified bin/Debug/net8.0/freelancerAPI.exe
Binary file not shown.
Binary file modified bin/Debug/net8.0/freelancerAPI.pdb
Binary file not shown.
Binary file modified obj/Debug/net8.0/apphost.exe
Binary file not shown.
Loading

0 comments on commit a3d8400

Please sign in to comment.