Skip to content

Commit

Permalink
Make MapperProfile to have the db context
Browse files Browse the repository at this point in the history
  • Loading branch information
Tudor3510 committed Feb 1, 2025
1 parent 8106d38 commit 20a5217
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
9 changes: 7 additions & 2 deletions backend-MT/Helpers/MapperProfile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoMapper;
using backend_MT.Data;
using backend_MT.Models;
using backend_MT.Models.DTOs;
using System.Net;
Expand All @@ -7,8 +8,12 @@ namespace backend_MT.Helpers
{
public class MapperProfile : Profile
{
public MapperProfile()
private readonly ApplicationDbContext _context;

public MapperProfile(ApplicationDbContext context)
{
_context = context;

CreateMap <Curs, CursDTO>();
CreateMap <CursDTO, Curs>();

Expand Down Expand Up @@ -36,7 +41,7 @@ public MapperProfile()
CreateMap <RaspunsTema, RaspunsTemaDTO> ();
CreateMap <RaspunsTemaDTO, RaspunsTema>();

CreateMap <Sedinta, SedintaDTO > ();
CreateMap<Sedinta, SedintaDTO > ();
CreateMap <SedintaDTO, Sedinta>();

CreateMap <Support, SupportDTO> ();
Expand Down
2 changes: 1 addition & 1 deletion backend-MT/Models/Tema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public class Tema
public string fisier { get; set; }
public int userId { get; set; }
public virtual User user { get; set; }
public ICollection<RaspunsTema>? raspunsuriTema { get; set; }
public virtual ICollection<RaspunsTema>? raspunsuriTema { get; set; }
}
}
15 changes: 13 additions & 2 deletions backend-MT/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@
using System.Text;
using System.Security.Claims;
using backend_MT.Helpers;
using AutoMapper;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();

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


builder.Services.AddIdentity<User, IdentityRole<int>>(options =>
{
Expand All @@ -64,6 +63,7 @@
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
}


// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
Expand Down Expand Up @@ -141,6 +141,17 @@
builder.Services.AddScoped<ISupportRepository, SupportRepository>();
builder.Services.AddScoped<ITemaRepository, TemaRepository>();

var mapperConfig = new MapperConfiguration(mc =>
{
// Resolve DbContext from the service provider
var serviceProvider = builder.Services.BuildServiceProvider();
var dbContext = serviceProvider.GetRequiredService<ApplicationDbContext>();
mc.AddProfile(new MapperProfile(dbContext)); // Create and add your profile manually
});

IMapper mapper = mapperConfig.CreateMapper();
builder.Services.AddSingleton(mapper);

// Services
builder.Services.AddScoped<ICursService, CursService>();
builder.Services.AddScoped<IDisponibilitateService, DisponibilitateService>();
Expand Down

0 comments on commit 20a5217

Please sign in to comment.