Skip to content

Commit

Permalink
Created base CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinOomenTheDeveloper committed Apr 18, 2024
1 parent f2c4a1b commit fd59120
Show file tree
Hide file tree
Showing 16 changed files with 465 additions and 41 deletions.
17 changes: 17 additions & 0 deletions MusicService/AutoMapperProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using AutoMapper;
using MusicService.Dtos.Song;
using MusicService.Models;

namespace MusicService
{
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
//All "Song" related mappings
CreateMap<Song, GetSongDto>();
CreateMap<AddSongDto, Song>();
CreateMap<UpdateSongDto, Song>();
}
}
}
76 changes: 76 additions & 0 deletions MusicService/Controllers/SongController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MusicService.Dtos.Song;
using MusicService.Services;
using MusicService.Services.Song;

namespace MusicService.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SongController : ControllerBase
{

private readonly ISongService _songService;

public SongController(ISongService songService)
{
_songService = songService;
}

[HttpGet("all")]
public async Task<ActionResult<ServiceResponse<List<GetSongDto>>>> GetAllSongs()
{
var response = await _songService.GetAllSongs();

if (response.Data == null)
return StatusCode(StatusCodes.Status404NotFound, response);

return StatusCode(StatusCodes.Status200OK, response);
}

[HttpGet("{songId}")]
public async Task<ActionResult<ServiceResponse<GetSongDto>>> GetSong(int songId)
{
var response = await _songService.GetSong(songId);

if (response.Data == null)
return StatusCode(StatusCodes.Status404NotFound, response);

return StatusCode(StatusCodes.Status200OK, response);
}

[HttpPost]
public async Task<ActionResult<ServiceResponse<GetSongDto>>> AddSong(AddSongDto request)
{
var songResponse = await _songService.AddSong(request);

if (!songResponse.Success)
return StatusCode(StatusCodes.Status404NotFound, songResponse);

return StatusCode(StatusCodes.Status201Created, songResponse);
}

[HttpPut]
public async Task<ActionResult<ServiceResponse<GetSongDto>>> UpdateSong(UpdateSongDto request)
{
var response = await _songService.UpdateSong(request);

if (response.Data == null)
return StatusCode(StatusCodes.Status404NotFound, response);

return StatusCode(StatusCodes.Status200OK, response);
}

/*[HttpDelete("{songId}")]
public async Task<ActionResult<ServiceResponse<GetSongDto>>> DeleteSong(int songId)
{
/*var response = await _songService;
if (response.Data == null)
return StatusCode(StatusCodes.Status404NotFound, response);
return StatusCode(StatusCodes.Status200OK, response);
}*/
}
}
33 changes: 0 additions & 33 deletions MusicService/Controllers/WeatherForecastController.cs

This file was deleted.

11 changes: 11 additions & 0 deletions MusicService/Data/DataContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
using Microsoft.EntityFrameworkCore;
using MusicService.Models;

namespace MusicService.Data
{
public class DataContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=music;Username=postgres;Password=example");

public DataContext()
{

}

public DataContext(DbContextOptions<DataContext> options) : base(options)
{

Expand All @@ -13,5 +22,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{

}

public DbSet<Song> song { get; set; }
}
}
9 changes: 9 additions & 0 deletions MusicService/Dtos/Song/AddSongDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MusicService.Dtos.Song
{
public class AddSongDto
{
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Adress { get; set; } = string.Empty;
}
}
10 changes: 10 additions & 0 deletions MusicService/Dtos/Song/GetSongDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MusicService.Dtos.Song
{
public class GetSongDto
{
public int Id { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Adress { get; set; } = string.Empty;
}
}
10 changes: 10 additions & 0 deletions MusicService/Dtos/Song/UpdateSongDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MusicService.Dtos.Song
{
public class UpdateSongDto
{
public int Id { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Adress { get; set; } = string.Empty;
}
}
54 changes: 54 additions & 0 deletions MusicService/Migrations/20240418225548_Initialcreate.Designer.cs

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

37 changes: 37 additions & 0 deletions MusicService/Migrations/20240418225548_Initialcreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace MusicService.Migrations
{
/// <inheritdoc />
public partial class Initialcreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "song",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "text", nullable: false),
Description = table.Column<string>(type: "text", nullable: false),
Adress = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_song", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "song");
}
}
}
51 changes: 51 additions & 0 deletions MusicService/Migrations/DataContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MusicService.Data;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace MusicService.Migrations
{
[DbContext(typeof(DataContext))]
partial class DataContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.11")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("MusicService.Models.Song", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");

NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));

b.Property<string>("Adress")
.IsRequired()
.HasColumnType("text");

b.Property<string>("Description")
.IsRequired()
.HasColumnType("text");

b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");

b.HasKey("Id");

b.ToTable("song");
});
#pragma warning restore 612, 618
}
}
}
16 changes: 16 additions & 0 deletions MusicService/Models/Song.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;

namespace MusicService.Models
{
public class Song
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public string Description { get; set; } = string.Empty;
[Required]
public string Adress { get; set; } = string.Empty;
}
}
13 changes: 6 additions & 7 deletions MusicService/MusicService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.18" />
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Dtos\" />
<Folder Include="Models\" />
</ItemGroup>

</Project>
9 changes: 8 additions & 1 deletion MusicService/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
using MusicService.Data;
using MusicService.Services.Song;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<DataContext>();
// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddAutoMapper(typeof(Program).Assembly);

builder.Services.AddScoped<ISongService, SongService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
Loading

0 comments on commit fd59120

Please sign in to comment.