Skip to content

Commit eee91a3

Browse files
committed
change history
1 parent a30c532 commit eee91a3

59 files changed

Lines changed: 984 additions & 48 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Films/Films.API/Controllers/FilmController.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,20 @@ public async Task<IActionResult> DeleteFilm(int filmId)
106106
/// <param name="filmDto">The DTO containing the film information to create.</param>
107107
/// <returns>Returns the ID of the created film.</returns>
108108
[HttpPost("create")]
109-
public async Task<ActionResult<int>> CreateFilm(FilmCreateDto filmDto)
109+
public async Task<ActionResult<int>> CreateFilm([FromBody] FilmCreateDto filmDto)
110110
{
111-
try
111+
if (!ModelState.IsValid)
112112
{
113-
var createdFilmId = await _filmService.CreateFilm(filmDto);
114-
return Ok(createdFilmId);
113+
return BadRequest(ModelState);
115114
}
116-
catch (Exception)
115+
116+
int createdFilmId = await _filmService.CreateFilm(filmDto);
117+
return Ok(createdFilmId);
118+
119+
/*catch (Exception)
117120
{
118121
return StatusCode(500, "An error occurred while processing your request.");
119-
}
122+
}*/
120123
}
121124

122125
/// <summary>

Films/Films.API/Films.API.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<ItemGroup>
1313
<PackageReference Include="AutoMapper" Version="12.0.1" />
1414
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
15+
<PackageReference Include="FluentValidation" Version="11.6.0" />
16+
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
17+
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.6.0" />
1518
<PackageReference Include="Kirel.Repositories" Version="1.0.2" />
1619
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0-preview3.23201.1" />
1720
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" />

Films/Films.API/Program.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System.Reflection;
22
using Films.Core;
33
using Films.Domain;
4+
using Films.DTOs;
45
using Films.Infrastructure;
6+
using FluentValidation;
7+
using FluentValidation.AspNetCore;
58
using Kirel.Repositories;
69
using Kirel.Repositories.Interfaces;
710
using Microsoft.EntityFrameworkCore;
@@ -11,17 +14,27 @@
1114
/*var authOptions = builder.Configuration.GetSection("AuthOptions").Get<AuthOptions>();*/
1215

1316
//taking connection string from appsettings.json
14-
var connectionString = builder.Configuration.GetConnectionString("PostgreConnection");
17+
var connectionString = builder.Configuration.GetConnectionString("SqlConnection");
1518
builder.Services.AddDbContext<FilmDbContext>(options =>
16-
options.UseNpgsql(connectionString));
19+
options.UseSqlServer(connectionString));
1720

1821
// Add services to the container.
1922
builder.Services.AddScoped<FilmService>();
2023
builder.Services.AddScoped<IKirelGenericEntityRepository<int, Film>, KirelGenericEntityFrameworkRepository<int, Film, FilmDbContext>>();
24+
builder.Services.AddScoped<IKirelGenericEntityRepository<int, Genre>, KirelGenericEntityFrameworkRepository<int, Genre, FilmDbContext>>();
2125

2226
//Add AutoMapper
27+
builder.Services.AddAutoMapper(cfg =>
28+
{
29+
cfg.CreateMap<FilmCreateDto, Film>()
30+
.ForMember(dest => dest.Genres, opt => opt.MapFrom(src => src.GenreNames!.Select(g => new Genre { Name = g })));
31+
}, Assembly.GetExecutingAssembly());
32+
33+
/*builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());*/
34+
builder.Services.AddFluentValidationAutoValidation();
35+
builder.Services.AddFluentValidationClientsideAdapters();
36+
builder.Services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
2337

24-
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
2538
builder.Services.AddControllers();
2639
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
2740
builder.Services.AddEndpointsApiExplorer();

Films/Films.Core/FilmMapper.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
using Films.Domain;
2-
using Films.DTOs;
3-
using AutoMapper;
4-
namespace Films.Core;
1+
using Films.Domain;
2+
using Films.DTOs;
3+
using AutoMapper;
4+
namespace Films.Core;
55

6-
/// <summary>
7-
/// Mapping Film for FilmDTO
8-
/// </summary>
9-
public class FilmMapper : Profile
10-
{
116
/// <summary>
12-
/// Films constructor
7+
/// Mapping Film for FilmDTO
138
/// </summary>
14-
public FilmMapper()
9+
public class FilmMapper : Profile
1510
{
16-
CreateMap<Film, FilmDto>().ReverseMap();
17-
CreateMap<Film, FilmCreateDto>().ReverseMap();
18-
CreateMap<Film, FilmUpdateDto>().ReverseMap();
19-
}
20-
}
11+
/// <summary>
12+
/// Films constructor
13+
/// </summary>
14+
public FilmMapper()
15+
{
16+
CreateMap<Film, FilmDto>().ReverseMap();
17+
CreateMap<FilmCreateDto, Film>()
18+
.ForMember(dest => dest.Genres, opt => opt.Ignore()); // Игнорируем маппинг списка жанров здесь
19+
20+
CreateMap<Film, FilmCreateDto>()
21+
.ForMember(dest => dest.GenreNames, opt => opt.MapFrom(src => src.Genres.Select(g => g.Name)));
22+
23+
CreateMap<Film, FilmUpdateDto>().ReverseMap();
24+
}
25+
}

Films/Films.Core/FilmService.cs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Films.DTOs;
44
using Kirel.Repositories.Interfaces;
55
using Kirel.Repositories.Sorts;
6+
using Microsoft.Extensions.DependencyInjection;
67

78
namespace Films.Core
89
{
@@ -13,16 +14,19 @@ public class FilmService
1314
{
1415
private readonly IKirelGenericEntityRepository<int, Film> _filmRepository;
1516
private readonly IMapper _mapper;
17+
private readonly IKirelGenericEntityRepository<int, Genre> _genreRepository;
1618

1719
/// <summary>
1820
/// Initializes a new instance of the <see cref="FilmService"/> class.
1921
/// </summary>
2022
/// <param name="filmRepository">The repository for accessing film data.</param>
2123
/// <param name="mapper">AutoMapper instance</param>
22-
public FilmService(IKirelGenericEntityRepository<int, Film> filmRepository, IMapper mapper)
24+
/// <param name="genreRepository"></param>
25+
public FilmService(IKirelGenericEntityRepository<int, Film> filmRepository, IMapper mapper, IKirelGenericEntityRepository<int, Genre> genreRepository)
2326
{
24-
this._filmRepository = filmRepository;
27+
_filmRepository = filmRepository;
2528
_mapper = mapper;
29+
_genreRepository = genreRepository;
2630
}
2731
/// <summary>
2832
/// Searches for films in the database by name.
@@ -32,39 +36,60 @@ public FilmService(IKirelGenericEntityRepository<int, Film> filmRepository, IMap
3236
public async Task<List<FilmDto>> SearchFilm(string filmName)
3337
{
3438
// Search for films in the database based on the provided film name.
35-
var existingFilm = await _filmRepository.GetList(m => m.Name != null && m.Name.Contains(filmName));
39+
var existingFilms = await _filmRepository.GetList(m => m.Name != null && m.Name.Contains(filmName));
3640

37-
var enumerable = existingFilm.ToList();
38-
if (existingFilm == null || !enumerable.Any())
41+
if (existingFilms == null || !existingFilms.Any())
3942
{
4043
throw new FilmNotFoundException($"Film with the title '{filmName}' was not found in the database.");
4144
}
4245

4346
// Map the retrieved films to DTOs for presentation.
44-
var filmsList = enumerable.Select(film => new FilmDto
47+
var filmsList = existingFilms.Select(film => new FilmDto
4548
{
4649
Id = film.Id,
4750
Name = film.Name,
4851
Rating = film.Rating,
4952
Description = film.Description,
5053
PosterURL = film.PosterUrl,
51-
Genres = film.Genres
54+
Genres = _mapper.Map<List<GenreDto>>(film.Genres)
5255
}).ToList();
5356

5457
return filmsList;
5558
}
59+
5660
/// <summary>
5761
/// Creates a new film.
5862
/// </summary>
59-
/// <param name="filmDto">The DTO containing the film information to create.</param>
63+
/// <param name="filmCreateDto">The DTO containing the film information to create.</param>
6064
/// <returns>The ID of the created film.</returns>
61-
public async Task<int> CreateFilm(FilmCreateDto filmDto)
62-
{
63-
// Map the DTO to an entity and insert it into the repository.
64-
var film = _mapper.Map<Film>(filmDto);
65-
var createdFilm = await _filmRepository.Insert(film);
66-
return createdFilm.Id;
67-
}
65+
public async Task<int> CreateFilm(FilmCreateDto filmCreateDto)
66+
{
67+
Film film = _mapper.Map<Film>(filmCreateDto);
68+
69+
// Добавьте жанры в фильм, если они указаны в filmCreateDto.GenreNames
70+
if (filmCreateDto.GenreNames != null)
71+
{
72+
foreach (string genreName in filmCreateDto.GenreNames)
73+
{
74+
var genre = await _genreRepository.GetList(m => m.Name != null && m.Name.Contains(genreName));
75+
if (genre.Any())
76+
{
77+
film.Genres.Add(genre.First()); // Выберите первый подходящий жанр
78+
}
79+
else
80+
{
81+
// Жанр с указанным названием не найден, создание нового жанра
82+
var newGenre = new Genre { Name = genreName };
83+
film.Genres.Add(newGenre);
84+
}
85+
}
86+
}
87+
88+
var createdFilm = await _filmRepository.Insert(film);
89+
return createdFilm.Id;
90+
}
91+
92+
6893

6994
/// <summary>
7095
/// Updates an existing film.

Films/Films.Core/Films.Core.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@
1818
<PackageReference Include="Kirel.Repositories" Version="1.0.2" />
1919
</ItemGroup>
2020

21+
<ItemGroup>
22+
<Reference Include="Microsoft.AspNetCore.Mvc.Core" />
23+
</ItemGroup>
24+
2125
</Project>

Films/Films.Core/GenreMapper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ public class GenreMapper : Profile
1414
public GenreMapper()
1515
{
1616
CreateMap<Genre, GenreDto>().ReverseMap();
17+
CreateMap<Genre, GenreCreateDto>().ReverseMap();
18+
CreateMap<Genre, GenreUpdateDto>().ReverseMap();
1719
}
1820
}

Films/Films.DTOs/FilmCreateDto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public class FilmCreateDto
2020
/// <summary>
2121
/// Gets or sets the list of genres associated with the film.
2222
/// </summary>
23-
public List<string>? Genres { get; set; }
23+
public List<string>? GenreNames { get; set; }
2424
/// <summary>
2525
/// Gets or sets the URL of the film's poster.
2626
/// </summary>
27-
public string? PosterURL { get; set; }
27+
public string? PosterUrl { get; set; }
2828
/// <summary>
2929
/// Gets or sets the timestamp when the film was created.
3030
/// </summary>

Films/Films.DTOs/FilmDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class FilmDto
2424
/// <summary>
2525
/// Gets or sets the list of genres associated with the film.
2626
/// </summary>
27-
public List<string>? Genres { get; set; }
27+
public List<GenreDto>? Genres { get; set; }
2828
/// <summary>
2929
/// Gets or sets the URL of the film's poster.
3030
/// </summary>

Films/Films.DTOs/FilmUpdateDto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public class FilmUpdateDto
2020
/// <summary>
2121
/// Gets or sets the list of genres associated with the film.
2222
/// </summary>
23-
public List<string>? Genres { get; set; }
23+
public List<GenreUpdateDto>? Genres { get; set; }
2424
/// <summary>
2525
/// Gets or sets the URL of the film's poster.
2626
/// </summary>
27-
public string? PosterURL { get; set; }
27+
public string? PosterUrl { get; set; }
2828
/// <summary>
2929
/// Gets or sets the timestamp when the film was created.
3030
/// </summary>

0 commit comments

Comments
 (0)