| using Diligent.WebAPI.Contracts.DTOs.Pattern; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Business.MappingProfiles | |||||
| { | |||||
| public class PatternMappingProfile : Profile | |||||
| { | |||||
| public PatternMappingProfile() | |||||
| { | |||||
| #region DTO to Model | |||||
| CreateMap<PatternCreateDto, Pattern>(); | |||||
| CreateMap<PatternUpdateDto, Pattern>(); | |||||
| #endregion | |||||
| #region Model to DTO | |||||
| CreateMap<Pattern, PatternResponseDto>(); | |||||
| #endregion | |||||
| } | |||||
| } | |||||
| } |
| using Diligent.WebAPI.Contracts.DTOs.Pattern; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Business.Services.Interfaces | |||||
| { | |||||
| public interface IPatternService | |||||
| { | |||||
| Task<List<PatternResponseDto>> GetAllAsync(); | |||||
| Task<PatternResponseDto> GetByIdAsync(int id); | |||||
| Task CreateAsync(PatternCreateDto patternCreateDto); | |||||
| Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id); | |||||
| Task DeleteAsync(int id); | |||||
| } | |||||
| } |
| { | { | ||||
| Task<List<SelectionLevelResponseWithDataDto>> GetAllAsync(); | Task<List<SelectionLevelResponseWithDataDto>> GetAllAsync(); | ||||
| Task<SelectionLevelResposneDto> GetByIdAsync(int id); | Task<SelectionLevelResposneDto> GetByIdAsync(int id); | ||||
| Task<SelectionLevel> GetByIdEntity(int id); | |||||
| List<SelectionLevelResponseWithDataDto> GetFilteredLevelsAsync(SelectionProcessFilterDto filters); | List<SelectionLevelResponseWithDataDto> GetFilteredLevelsAsync(SelectionProcessFilterDto filters); | ||||
| } | } | ||||
| } | } |
| using Diligent.WebAPI.Contracts.DTOs.Pattern; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Business.Services | |||||
| { | |||||
| public class PatternService : IPatternService | |||||
| { | |||||
| private readonly DatabaseContext _context; | |||||
| private readonly IMapper _mapper; | |||||
| private readonly ISelectionLevelService _selectionLevelService; | |||||
| private readonly ILogger<PatternService> _logger; | |||||
| public PatternService(DatabaseContext context, IMapper mapper, ISelectionLevelService selectionLevelService, ILogger<PatternService> logger) | |||||
| { | |||||
| _context = context; | |||||
| _mapper = mapper; | |||||
| _selectionLevelService = selectionLevelService; | |||||
| _logger = logger; | |||||
| } | |||||
| public async Task<List<PatternResponseDto>> GetAllAsync() | |||||
| { | |||||
| _logger.LogInformation("Start getting all Patterns"); | |||||
| _logger.LogInformation("Getting data from DB"); | |||||
| var fromDb = await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync(); | |||||
| _logger.LogInformation($"Received {fromDb.Count} patterns from db."); | |||||
| _logger.LogInformation($"Mapping received patterns to PatternResponseDto"); | |||||
| var result = _mapper.Map<List<PatternResponseDto>>(fromDb); | |||||
| _logger.LogInformation($"Patterns has been mapped and received to client: {result.Count} mapped patterns"); | |||||
| return result; | |||||
| } | |||||
| public async Task<PatternResponseDto> GetByIdAsync(int id) | |||||
| { | |||||
| _logger.LogInformation($"Start searching Pattern with id = {id}"); | |||||
| var pattern = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Id == id).FirstOrDefaultAsync(); | |||||
| if (pattern is null) | |||||
| { | |||||
| _logger.LogError($"Pattern with id = {id} not found"); | |||||
| throw new EntityNotFoundException("Pattern not found"); | |||||
| } | |||||
| _logger.LogInformation($"Mapping Pattern with id = {id}"); | |||||
| PatternResponseDto result = _mapper.Map<PatternResponseDto>(pattern); | |||||
| _logger.LogInformation($"Pattern with id = {id} mapped successfully"); | |||||
| return result; | |||||
| } | |||||
| public async Task CreateAsync(PatternCreateDto patternCreateDto) | |||||
| { | |||||
| _logger.LogInformation($"Start creating Pattern"); | |||||
| _logger.LogInformation($"Check is Pattern in database"); | |||||
| var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternCreateDto.Title && x.SelectionLevelId == patternCreateDto.SelectionLevelId).FirstOrDefaultAsync(); | |||||
| if (patternExists is not null) | |||||
| { | |||||
| _logger.LogError($"Pattern already exists in database"); | |||||
| throw new EntityNotFoundException("Pattern already exists in database"); | |||||
| } | |||||
| _logger.LogError($"Pattern are not in database"); | |||||
| _logger.LogInformation($"Mapping PatternCreateDto to model"); | |||||
| var pattern = _mapper.Map<Pattern>(patternCreateDto); | |||||
| _logger.LogInformation($"Pattern mapped successfully"); | |||||
| _logger.LogInformation($"Start searching SelectionLevel with id = {patternCreateDto.SelectionLevelId}"); | |||||
| var selectionLevel = await _selectionLevelService.GetByIdEntity(patternCreateDto.SelectionLevelId); | |||||
| if(selectionLevel == null) | |||||
| { | |||||
| _logger.LogError($"SelectionLevel with id = {patternCreateDto.SelectionLevelId} not found"); | |||||
| throw new EntityNotFoundException("Selection level not found"); | |||||
| } | |||||
| _logger.LogInformation($"Add founded SelectionLevel to pattern"); | |||||
| pattern.SelectionLevel = selectionLevel; | |||||
| await _context.AddAsync(pattern); | |||||
| _logger.LogInformation($"Saving Ad to db..."); | |||||
| var result = _context.SaveChangesAsync(); | |||||
| _logger.LogInformation($"Saved Ad to db..."); | |||||
| await result; | |||||
| } | |||||
| public async Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id) | |||||
| { | |||||
| _logger.LogInformation($"Start updating Pattern"); | |||||
| _logger.LogInformation($"Check is Pattern in database"); | |||||
| var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternUpdateDto.Title && x.SelectionLevelId == patternUpdateDto.SelectionLevelId).FirstOrDefaultAsync(); | |||||
| if (patternExists is not null) | |||||
| { | |||||
| _logger.LogError($"Pattern already exists in database"); | |||||
| throw new EntityNotFoundException("Pattern already exists in database"); | |||||
| } | |||||
| _logger.LogInformation($"Start searching Pattern with id = {id}"); | |||||
| var pattern = await _context.Patterns.Where(x => x.Id == id).FirstOrDefaultAsync(); | |||||
| if (pattern is null) | |||||
| { | |||||
| _logger.LogError($"Pattern with id = {id} not found"); | |||||
| throw new EntityNotFoundException("Pattern not found"); | |||||
| } | |||||
| _logger.LogInformation($"Mapping Pattern with id = {id}"); | |||||
| _mapper.Map(patternUpdateDto, pattern); | |||||
| _logger.LogInformation($"Pattern with id = {id} mapped successfully"); | |||||
| _logger.LogInformation($"Start searching SelectionLevel with id = {patternUpdateDto.SelectionLevelId}"); | |||||
| var selectionLevel = await _selectionLevelService.GetByIdEntity(patternUpdateDto.SelectionLevelId); | |||||
| if (selectionLevel == null) | |||||
| { | |||||
| _logger.LogError($"SelectionLevel with id = {patternUpdateDto.SelectionLevelId} not found"); | |||||
| throw new EntityNotFoundException("Selection level not found"); | |||||
| } | |||||
| _logger.LogInformation($"Add founded SelectionLevel to pattern"); | |||||
| pattern.SelectionLevel = selectionLevel; | |||||
| _context.Entry(pattern).State = EntityState.Modified; | |||||
| var result = _context.SaveChangesAsync(); | |||||
| _logger.LogInformation($"Pattern saved to DB"); | |||||
| await result; | |||||
| } | |||||
| public async Task DeleteAsync(int id) | |||||
| { | |||||
| _logger.LogInformation($"Start searching Pattern with id = {id}"); | |||||
| var pattern = await _context.Patterns.FindAsync(id); | |||||
| if (pattern is null) | |||||
| { | |||||
| _logger.LogError($"Pattern with id = {id} not found"); | |||||
| throw new EntityNotFoundException("Pattern not found"); | |||||
| } | |||||
| _context.Patterns.Remove(pattern); | |||||
| var result = _context.SaveChangesAsync(); | |||||
| _logger.LogInformation($"Ad saved to DB"); | |||||
| await result; | |||||
| } | |||||
| } | |||||
| } |
| } | } | ||||
| public async Task<SelectionLevel> GetByIdEntity(int id) | |||||
| { | |||||
| _logger.LogInformation($"Start searching Level with id = {id}"); | |||||
| var sl = await _context.SelectionLevels.FindAsync(id); | |||||
| if (sl is null) | |||||
| { | |||||
| _logger.LogError($"Level with id = {id} not found"); | |||||
| throw new EntityNotFoundException("Selection level not found"); | |||||
| } | |||||
| _logger.LogError($"Level with id = {id} found and returned to client"); | |||||
| return sl; | |||||
| } | |||||
| public List<SelectionLevelResponseWithDataDto> GetFilteredLevelsAsync(SelectionProcessFilterDto filters) | public List<SelectionLevelResponseWithDataDto> GetFilteredLevelsAsync(SelectionProcessFilterDto filters) | ||||
| { | { | ||||
| _logger.LogInformation("Start getting filtered selection levels"); | _logger.LogInformation("Start getting filtered selection levels"); |
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.Pattern | |||||
| { | |||||
| public class PatternCreateDto | |||||
| { | |||||
| public string Title { get; set; } | |||||
| public DateTime? CreatedAt { get; set; } = DateTime.Now; | |||||
| public int SelectionLevelId { get; set; } | |||||
| public string Message { get; set; } | |||||
| } | |||||
| } |
| using Diligent.WebAPI.Contracts.DTOs.SelectionLevel; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.Pattern | |||||
| { | |||||
| public class PatternResponseDto | |||||
| { | |||||
| public int Id { get; set; } | |||||
| public string Title { get; set; } | |||||
| public DateTime CreatedAt { get; set; } = DateTime.Now; | |||||
| public SelectionLevelResposneDto SelectionLevel { get; set; } | |||||
| public string Message { get; set; } | |||||
| } | |||||
| } |
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.Pattern | |||||
| { | |||||
| public class PatternUpdateDto | |||||
| { | |||||
| public string Title { get; set; } | |||||
| public DateTime CreatedAt { get; set; } = DateTime.Now; | |||||
| public int SelectionLevelId { get; set; } | |||||
| public string Message { get; set; } | |||||
| } | |||||
| } |
| public DbSet<Comment> Comments { get; set; } | public DbSet<Comment> Comments { get; set; } | ||||
| public DbSet<SelectionLevel> SelectionLevels { get; set; } | public DbSet<SelectionLevel> SelectionLevels { get; set; } | ||||
| public DbSet<SelectionProcess> SelectionProcesses { get; set; } | public DbSet<SelectionProcess> SelectionProcesses { get; set; } | ||||
| public DbSet<Pattern> Patterns { get; set; } | |||||
| public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { } | public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { } |
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.ComponentModel.DataAnnotations.Schema; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Data.Entities | |||||
| { | |||||
| public class Pattern | |||||
| { | |||||
| public int Id { get; set; } | |||||
| public string Title { get; set; } | |||||
| public DateTime CreatedAt { get; set; } = DateTime.Now; | |||||
| [ForeignKey(nameof(SelectionLevel))] | |||||
| public int SelectionLevelId { get; set; } | |||||
| public SelectionLevel SelectionLevel { get; set; } | |||||
| public string Message { get; set; } | |||||
| } | |||||
| } |
| using System; | |||||
| using Microsoft.EntityFrameworkCore.Migrations; | |||||
| #nullable disable | |||||
| namespace Diligent.WebAPI.Data.Migrations | |||||
| { | |||||
| public partial class AddedPattern : Migration | |||||
| { | |||||
| protected override void Up(MigrationBuilder migrationBuilder) | |||||
| { | |||||
| migrationBuilder.CreateTable( | |||||
| name: "Patterns", | |||||
| columns: table => new | |||||
| { | |||||
| Id = table.Column<int>(type: "int", nullable: false) | |||||
| .Annotation("SqlServer:Identity", "1, 1"), | |||||
| Title = table.Column<string>(type: "nvarchar(max)", nullable: false), | |||||
| CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false), | |||||
| SelectionLevelId = table.Column<int>(type: "int", nullable: false), | |||||
| Message = table.Column<string>(type: "nvarchar(max)", nullable: false) | |||||
| }, | |||||
| constraints: table => | |||||
| { | |||||
| table.PrimaryKey("PK_Patterns", x => x.Id); | |||||
| table.ForeignKey( | |||||
| name: "FK_Patterns_SelectionLevels_SelectionLevelId", | |||||
| column: x => x.SelectionLevelId, | |||||
| principalTable: "SelectionLevels", | |||||
| principalColumn: "Id", | |||||
| onDelete: ReferentialAction.Cascade); | |||||
| }); | |||||
| migrationBuilder.CreateIndex( | |||||
| name: "IX_Patterns_SelectionLevelId", | |||||
| table: "Patterns", | |||||
| column: "SelectionLevelId"); | |||||
| } | |||||
| protected override void Down(MigrationBuilder migrationBuilder) | |||||
| { | |||||
| migrationBuilder.DropTable( | |||||
| name: "Patterns"); | |||||
| } | |||||
| } | |||||
| } |
| b.ToTable("Insurers"); | b.ToTable("Insurers"); | ||||
| }); | }); | ||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Pattern", b => | |||||
| { | |||||
| b.Property<int>("Id") | |||||
| .ValueGeneratedOnAdd() | |||||
| .HasColumnType("int"); | |||||
| SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | |||||
| b.Property<DateTime>("CreatedAt") | |||||
| .HasColumnType("datetime2"); | |||||
| b.Property<string>("Message") | |||||
| .IsRequired() | |||||
| .HasColumnType("nvarchar(max)"); | |||||
| b.Property<int>("SelectionLevelId") | |||||
| .HasColumnType("int"); | |||||
| b.Property<string>("Title") | |||||
| .IsRequired() | |||||
| .HasColumnType("nvarchar(max)"); | |||||
| b.HasKey("Id"); | |||||
| b.HasIndex("SelectionLevelId"); | |||||
| b.ToTable("Patterns"); | |||||
| }); | |||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.RefreshToken", b => | modelBuilder.Entity("Diligent.WebAPI.Data.Entities.RefreshToken", b => | ||||
| { | { | ||||
| b.Property<int>("Id") | b.Property<int>("Id") | ||||
| b.Navigation("InsuranceCompany"); | b.Navigation("InsuranceCompany"); | ||||
| }); | }); | ||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Pattern", b => | |||||
| { | |||||
| b.HasOne("Diligent.WebAPI.Data.Entities.SelectionLevel", "SelectionLevel") | |||||
| .WithMany() | |||||
| .HasForeignKey("SelectionLevelId") | |||||
| .OnDelete(DeleteBehavior.Cascade) | |||||
| .IsRequired(); | |||||
| b.Navigation("SelectionLevel"); | |||||
| }); | |||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.RefreshToken", b => | modelBuilder.Entity("Diligent.WebAPI.Data.Entities.RefreshToken", b => | ||||
| { | { | ||||
| b.HasOne("Diligent.WebAPI.Data.Entities.User", "User") | b.HasOne("Diligent.WebAPI.Data.Entities.User", "User") |
| using Diligent.WebAPI.Contracts.DTOs.Pattern; | |||||
| namespace Diligent.WebAPI.Host.Controllers.V1 | |||||
| { | |||||
| [ApiVersion("1.0")] | |||||
| [Route("v{version:apiVersion}/patterns")] | |||||
| [ApiController] | |||||
| public class PatternsController : ControllerBase | |||||
| { | |||||
| private readonly IPatternService _patternService; | |||||
| public PatternsController(IPatternService patternService) | |||||
| { | |||||
| _patternService = patternService; | |||||
| } | |||||
| [HttpGet] | |||||
| public async Task<IActionResult> GetAll() => | |||||
| Ok(await _patternService.GetAllAsync()); | |||||
| [HttpGet("{id}")] | |||||
| public async Task<IActionResult> GetById([FromRoute] int id) => | |||||
| Ok(await _patternService.GetByIdAsync(id)); | |||||
| [HttpPost] | |||||
| public async Task<IActionResult> Create([FromBody] PatternCreateDto request) | |||||
| { | |||||
| await _patternService.CreateAsync(request); | |||||
| return StatusCode((int)HttpStatusCode.Created); | |||||
| } | |||||
| [HttpPut("{id}")] | |||||
| public async Task<IActionResult> Update([FromBody]PatternUpdateDto request, [FromRoute]int id) | |||||
| { | |||||
| await _patternService.UpdateAsync(request, id); | |||||
| return Ok(); | |||||
| } | |||||
| [HttpDelete("{id}")] | |||||
| public async Task<IActionResult> DeletePattern([FromRoute] int id) | |||||
| { | |||||
| await _patternService.DeleteAsync(id); | |||||
| return NoContent(); | |||||
| } | |||||
| } | |||||
| } |
| services.AddScoped<IAdService, AdService>(); | services.AddScoped<IAdService, AdService>(); | ||||
| services.AddScoped<ITechnologyService, TechnologyService>(); | services.AddScoped<ITechnologyService, TechnologyService>(); | ||||
| services.AddScoped<ICommentService, CommentService>(); | services.AddScoped<ICommentService, CommentService>(); | ||||
| services.AddScoped<IPatternService, PatternService>(); | |||||
| services.AddScoped<IScheduleService, ScheduleService>(); | services.AddScoped<IScheduleService, ScheduleService>(); | ||||
| } | } | ||||