Commit baa1603a authored by peter-stefunko's avatar peter-stefunko
Browse files

formatting and slight restructalization

parent 244ca11a
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Kollectionized.Api.Data;
using Kollectionized.Api.Dtos;
using Kollectionized.Api.Models;

namespace Kollectionized.Api.Controllers;

@@ -25,7 +24,7 @@ public class AccountUpdateController(AppDbContext context) : ControllerBase
            user.Username = dto.NewUsername;
        }

        user.Bio = dto.Bio ?? user.Bio;
        user.Bio = dto.Bio;
        context.Users.Update(user);
        await context.SaveChangesAsync();

+10 −37
Original line number Diff line number Diff line
@@ -55,13 +55,14 @@ public class AuthController(AppDbContext context) : ControllerBase
            if (!BCrypt.Net.BCrypt.Verify(dto.Password, user.PasswordHash))
                return Unauthorized("Invalid credentials.");

            var userDto = new UserDto(
                Id: user.Id,
                Username: user.Username,
                CreatedAt: user.CreatedAt,
                LastUsername: user.LastUsername ?? string.Empty,
                Bio: user.Bio ?? string.Empty
            );
            var userDto = new UserDto
            {
                Id = user.Id,
                Username = user.Username,
                CreatedAt = user.CreatedAt,
                LastUsername = user.LastUsername ?? string.Empty,
                Bio = user.Bio ?? string.Empty,
            };

            return Ok(userDto);
        }
@@ -72,12 +73,12 @@ public class AuthController(AppDbContext context) : ControllerBase
    }

    [HttpDelete("user/{username}")]
    public async Task<IActionResult> DeleteAccount(string username, [FromBody] PasswordOnlyDto dto)
    public async Task<IActionResult> DeleteAccount(string username, string password)
    {
        try
        {
            var user = await context.Users.FirstOrDefaultAsync(u => u.Username == username);
            if (user == null || !BCrypt.Net.BCrypt.Verify(dto.Password, user.PasswordHash))
            if (user == null || !BCrypt.Net.BCrypt.Verify(password, user.PasswordHash))
                return Unauthorized("Invalid password.");

            if (user.Username.StartsWith("[del-"))
@@ -104,32 +105,4 @@ public class AuthController(AppDbContext context) : ControllerBase
            return StatusCode(500, "Something went wrong on the server");
        }
    }

    [HttpPut("change-username")]
    public async Task<IActionResult> ChangeUsername([FromBody] UsernameChangeDto dto)
    {
        try
        {
            var user = await context.Users.FirstOrDefaultAsync(u => u.Username == dto.CurrentUsername);
            if (user == null || !BCrypt.Net.BCrypt.Verify(dto.Password, user.PasswordHash))
                return Unauthorized("Invalid username or password.");

            if (!UsernameValidation.IsValid(dto.NewUsername, out var error))
                return BadRequest(error);

            var exists = await context.Users.AnyAsync(u => u.Username == dto.NewUsername && u.Id != user.Id);
            if (exists)
                return BadRequest("That username is already taken.");

            user.Username = dto.NewUsername;
            context.Users.Update(user);
            await context.SaveChangesAsync();

            return Ok(new { message = "Name changed successfully." });
        }
        catch
        {
            return StatusCode(500, "Something went wrong on the server.");
        }
    }
}
 No newline at end of file
+6 −6
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ public class UserCardController(AppDbContext context) : ControllerBase
                CurrentOwner = user.Id,
                Grade = dto.Grade,
                GradingCompany = dto.GradingCompany ?? string.Empty,
                Notes = dto.Notes ?? string.Empty,
                Notes = dto.Notes,
                CreatedAt = DateTime.UtcNow
            };

@@ -62,7 +62,7 @@ public class UserCardController(AppDbContext context) : ControllerBase
        }
    }

    [HttpPut("{id}")]
    [HttpPut("{id:guid}")]
    public async Task<IActionResult> UpdateCardInstance(string username, Guid id, [FromBody] CardInstanceUpdateDto dto)
    {
        try
@@ -77,7 +77,7 @@ public class UserCardController(AppDbContext context) : ControllerBase

            instance.Grade = dto.Grade;
            instance.GradingCompany = dto.GradingCompany ?? string.Empty;
            instance.Notes = dto.Notes ?? string.Empty;
            instance.Notes = dto.Notes;

            await context.SaveChangesAsync();
            return Ok(new { message = "Card instance updated." });
@@ -88,13 +88,13 @@ public class UserCardController(AppDbContext context) : ControllerBase
        }
    }

    [HttpDelete("{id}")]
    public async Task<IActionResult> DeleteCardInstance(string username, Guid id, [FromBody] PasswordOnlyDto dto)
    [HttpDelete("{id:guid}")]
    public async Task<IActionResult> DeleteCardInstance(string username, Guid id, string password)
    {
        try
        {
            var user = await context.Users.FirstOrDefaultAsync(u => u.Username == username);
            if (user == null || !BCrypt.Net.BCrypt.Verify(dto.Password, user.PasswordHash))
            if (user == null || !BCrypt.Net.BCrypt.Verify(password, user.PasswordHash))
                return Unauthorized("Invalid credentials.");

            var instance =
+3 −3
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ public class UsersController(AppDbContext context) : ControllerBase
            var users = await context.Users
                .Where(u => !u.Username.StartsWith("[del-"))
                .OrderBy(u => u.Username)
                .Select(u => new UserDto(u.Id, u.Username, u.CreatedAt, u.LastUsername, u.Bio))
                .Select(u => new UserDto{Id = u.Id, Username = u.Username, CreatedAt = u.CreatedAt, LastUsername = u.LastUsername, Bio = u.Bio})
                .ToListAsync();

            return Ok(users);
@@ -28,14 +28,14 @@ public class UsersController(AppDbContext context) : ControllerBase
        }
    }
    
    [HttpGet("{id}")]
    [HttpGet("{id:guid}")]
    public async Task<ActionResult<UserDto>> GetUserByUsername(Guid id)
    {
        try
        {
            var user = await context.Users
                .Where(u => u.Id == id && !u.Username.StartsWith("[del-"))
                .Select(u => new UserDto(u.Id, u.Username, u.CreatedAt, u.LastUsername, u.Bio))
                .Select(u => new UserDto{Id = u.Id, Username = u.Username, CreatedAt = u.CreatedAt, LastUsername = u.LastUsername, Bio = u.Bio})
                .FirstOrDefaultAsync();

            return user is null ? NotFound("User not found.") : Ok(user);
+1 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(op
    public DbSet<PokemonDeck> PokemonDecks => Set<PokemonDeck>();
    public DbSet<PokemonCard> PokemonCards => Set<PokemonCard>();
    public DbSet<CardInstance> PokemonCardInstances => Set<CardInstance>();
    public DbSet<PokemonSet> PokemonSets { get; set; }
    public DbSet<PokemonSet> PokemonSets => Set<PokemonSet>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
Loading