diff --git a/VideoGameCharacterApi/Controllers/VideoGameCharactersController.cs b/VideoGameCharacterApi/Controllers/VideoGameCharactersController.cs index b7df55d..9e0dc7d 100644 --- a/VideoGameCharacterApi/Controllers/VideoGameCharactersController.cs +++ b/VideoGameCharacterApi/Controllers/VideoGameCharactersController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using VideoGameCharacterApi.Dtos; using VideoGameCharacterApi.Models; using VideoGameCharacterApi.Services; @@ -11,11 +12,11 @@ namespace VideoGameCharacterApi.Controllers { [HttpGet] - public async Task>> GetCharacters() + public async Task>> GetCharacters() => Ok(await service.GetCharactersAsync()); [HttpGet("{id}")] - public async Task> GetCharacter(int id) + public async Task> GetCharacter(int id) { var character = await service.GetCharacterByIdAsync(id); return character is null ? NotFound("Character with given id was not found") : Ok(character); diff --git a/VideoGameCharacterApi/Dtos/CharacterResponse.cs b/VideoGameCharacterApi/Dtos/CharacterResponse.cs new file mode 100644 index 0000000..978fd76 --- /dev/null +++ b/VideoGameCharacterApi/Dtos/CharacterResponse.cs @@ -0,0 +1,10 @@ +namespace VideoGameCharacterApi.Dtos +{ + public class CharacterResponse + { + //public int Id { get; set; } + public string Name { get; set; } = string.Empty; + public string Game { get; set; } = string.Empty; + public string Role { get; set; } = string.Empty; + } +} diff --git a/VideoGameCharacterApi/Migrations/20260207133430_initial.Designer.cs b/VideoGameCharacterApi/Migrations/20260207133430_initial.Designer.cs new file mode 100644 index 0000000..936fb03 --- /dev/null +++ b/VideoGameCharacterApi/Migrations/20260207133430_initial.Designer.cs @@ -0,0 +1,54 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using VideoGameCharacterApi.Data; + +#nullable disable + +namespace VideoGameCharacterApi.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20260207133430_initial")] + partial class initial + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "10.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("VideoGameCharacterApi.Models.Character", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Game") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Role") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Characters"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/VideoGameCharacterApi/Migrations/20260207133430_initial.cs b/VideoGameCharacterApi/Migrations/20260207133430_initial.cs new file mode 100644 index 0000000..605edb8 --- /dev/null +++ b/VideoGameCharacterApi/Migrations/20260207133430_initial.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace VideoGameCharacterApi.Migrations +{ + /// + public partial class initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Characters", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Game = table.Column(type: "nvarchar(max)", nullable: false), + Role = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Characters", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Characters"); + } + } +} diff --git a/VideoGameCharacterApi/Migrations/ApplicationDbContextModelSnapshot.cs b/VideoGameCharacterApi/Migrations/ApplicationDbContextModelSnapshot.cs new file mode 100644 index 0000000..a3d861f --- /dev/null +++ b/VideoGameCharacterApi/Migrations/ApplicationDbContextModelSnapshot.cs @@ -0,0 +1,51 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using VideoGameCharacterApi.Data; + +#nullable disable + +namespace VideoGameCharacterApi.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + partial class ApplicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "10.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("VideoGameCharacterApi.Models.Character", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Game") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Role") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Characters"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/VideoGameCharacterApi/Program.cs b/VideoGameCharacterApi/Program.cs index 2a1acb3..9b5e7dc 100644 --- a/VideoGameCharacterApi/Program.cs +++ b/VideoGameCharacterApi/Program.cs @@ -1,4 +1,6 @@ +using Microsoft.EntityFrameworkCore; using Scalar.AspNetCore; +using VideoGameCharacterApi.Data; using VideoGameCharacterApi.Services; var builder = WebApplication.CreateBuilder(args); @@ -9,7 +11,7 @@ builder.Services.AddControllers(); // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi(); -builder.services.AddDbContext(options => +builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); builder.Services.AddScoped(); diff --git a/VideoGameCharacterApi/Services/IVideoGameCharacterService.cs b/VideoGameCharacterApi/Services/IVideoGameCharacterService.cs index 6d4f592..bb299b6 100644 --- a/VideoGameCharacterApi/Services/IVideoGameCharacterService.cs +++ b/VideoGameCharacterApi/Services/IVideoGameCharacterService.cs @@ -1,12 +1,13 @@ -using VideoGameCharacterApi.Models; +using VideoGameCharacterApi.Dtos; +using VideoGameCharacterApi.Models; namespace VideoGameCharacterApi.Services; public interface IVideoGameCharacterService { - Task> GetCharactersAsync(); - Task GetCharacterByIdAsync(int id); - Task AddCharacterAsync(Character character); + Task> GetCharactersAsync(); + Task GetCharacterByIdAsync(int id); + Task AddCharacterAsync(Character character); Task UpdateCharacterAsync(int id,Character character); Task DeleteCharacterAsync(int id); } diff --git a/VideoGameCharacterApi/Services/VideoGameCharacterService.cs b/VideoGameCharacterApi/Services/VideoGameCharacterService.cs index bcf1e2d..4c1c083 100644 --- a/VideoGameCharacterApi/Services/VideoGameCharacterService.cs +++ b/VideoGameCharacterApi/Services/VideoGameCharacterService.cs @@ -1,19 +1,21 @@ -using VideoGameCharacterApi.Models; +using Microsoft.EntityFrameworkCore; +using VideoGameCharacterApi.Data; +using VideoGameCharacterApi.Dtos; +using VideoGameCharacterApi.Models; namespace VideoGameCharacterApi.Services { public class VideoGameCharacterService : IVideoGameCharacterService { - static List characters = new List - { - new Character { Id = 1, Name = "Mario", Game = "Super Mario Bros.", Role = "hero" }, - new Character { Id = 2, Name = "Link", Game = "The Legend of Zelda", Role = "hero" }, - new Character { Id = 3, Name = "Bowser", Game = "Super Mario Bros.", Role = "villain" }, - new Character { Id = 4, Name = "Zelda", Game = "The Legend of Zelda", Role = "princess" }, + private readonly ApplicationDbContext _context; - }; + public VideoGameCharacterService(ApplicationDbContext context) + { + _context = context; + } - public Task AddCharacterAsync(Character character) + + public Task AddCharacterAsync(Character character) { throw new NotImplementedException(); } @@ -23,15 +25,35 @@ namespace VideoGameCharacterApi.Services throw new NotImplementedException(); } - public async Task GetCharacterByIdAsync(int id) - => await Task.FromResult(characters.FirstOrDefault(c => c.Id == id)); + public async Task GetCharacterByIdAsync(int id) + { + var result = await _context.Characters + .Where(c => c.Id == id) + .Select(c => new CharacterResponse + { + Name = c.Name, + Game = c.Game, + Role = c.Role + }) + .FirstOrDefaultAsync(); + return result; + } - public async Task> GetCharactersAsync() - => await Task.FromResult(characters); + public async Task> GetCharactersAsync() + => await _context.Characters.Select(c => new CharacterResponse + { + Name = c.Name, + Game = c.Game, + Role = c.Role + }).ToListAsync(); public Task UpdateCharacterAsync(int id, Character character) { throw new NotImplementedException(); } - } + }; + + + + } diff --git a/VideoGameCharacterApi/appsettings.json b/VideoGameCharacterApi/appsettings.json index d2a3663..9ceaf7d 100644 --- a/VideoGameCharacterApi/appsettings.json +++ b/VideoGameCharacterApi/appsettings.json @@ -1,7 +1,6 @@ { "ConnectionStrings": { - "DefaultConnection": "" - "Server=TFOACERLAP;Database=VideoGameCharactersDB;Trusted_Connection=True;Encrypt=no;MultipleActiveResultSets=true\"\"", + "DefaultConnection": "Server=TFOACERLAP;Database=VideoGameCharactersDB;Trusted_Connection=True;Encrypt=no;MultipleActiveResultSets=true" }, "Logging": { "LogLevel": {