Procházet zdrojové kódy

Done 1589, 1591, 1790, 1794, 1796, 1797, 1798, 1799 tasks

pull/79/head
Ermin Bronja před 3 roky
rodič
revize
239faa8769

+ 24
- 0
Diligent.WebAPI.Business/MappingProfiles/PatternMappingProfile.cs Zobrazit soubor

@@ -0,0 +1,24 @@
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
}
}
}

+ 22
- 0
Diligent.WebAPI.Business/Services/Interfaces/IPatternService.cs Zobrazit soubor

@@ -0,0 +1,22 @@
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);
}
}

+ 1
- 0
Diligent.WebAPI.Business/Services/Interfaces/ISelectionLevelService.cs Zobrazit soubor

@@ -8,6 +8,7 @@ namespace Diligent.WebAPI.Business.Services.Interfaces
{
Task<List<SelectionLevelResponseWithDataDto>> GetAllAsync();
Task<SelectionLevelResposneDto> GetByIdAsync(int id);
Task<SelectionLevel> GetByIdEntity(int id);
List<SelectionLevelResponseWithDataDto> GetFilteredLevelsAsync(SelectionProcessFilterDto filters);
}
}

+ 92
- 0
Diligent.WebAPI.Business/Services/PatternService.cs Zobrazit soubor

@@ -0,0 +1,92 @@
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;

public PatternService(DatabaseContext context, IMapper mapper, ISelectionLevelService selectionLevelService)
{
_context = context;
_mapper = mapper;
_selectionLevelService = selectionLevelService;
}

public async Task<List<PatternResponseDto>> GetAllAsync()
{
return _mapper.Map<List<PatternResponseDto>>(await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync());
}

public async Task<PatternResponseDto> GetByIdAsync(int id)
{
var pattern = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Id == id).FirstOrDefaultAsync();

if (pattern is null)
throw new EntityNotFoundException("Pattern not found");

return _mapper.Map<PatternResponseDto>(pattern);
}

public async Task CreateAsync(PatternCreateDto patternCreateDto)
{
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)
throw new EntityNotFoundException("Pattern already exists in database");

var pattern = _mapper.Map<Pattern>(patternCreateDto);

var selectionLevel = await _selectionLevelService.GetByIdEntity(patternCreateDto.SelectionLevelId);

if(selectionLevel == null)
throw new EntityNotFoundException("Selection level not found");

pattern.SelectionLevel = selectionLevel;

await _context.AddAsync(pattern);

await _context.SaveChangesAsync();
}

public async Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id)
{
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)
throw new EntityNotFoundException("Pattern already exists in database");

var pattern = await _context.Patterns.Where(x => x.Id == id).FirstOrDefaultAsync();

if (pattern is null)
throw new EntityNotFoundException("Pattern not found");

_mapper.Map(patternUpdateDto, pattern);

var selectionLevel = await _selectionLevelService.GetByIdEntity(patternUpdateDto.SelectionLevelId);

pattern.SelectionLevel = selectionLevel;

_context.Entry(pattern).State = EntityState.Modified;
await _context.SaveChangesAsync();
}

public async Task DeleteAsync(int id)
{
var pattern = await _context.Patterns.FindAsync(id);

if (pattern is null)
throw new EntityNotFoundException("Pattern not found");

_context.Patterns.Remove(pattern);
await _context.SaveChangesAsync();
}
}
}

+ 10
- 0
Diligent.WebAPI.Business/Services/SelectionLevelService.cs Zobrazit soubor

@@ -47,6 +47,16 @@

}

public async Task<SelectionLevel> GetByIdEntity(int id)
{
var sl = await _context.SelectionLevels.FindAsync(id);

if (sl is null)
throw new EntityNotFoundException("Selection level not found");

return sl;
}

public List<SelectionLevelResponseWithDataDto> GetFilteredLevelsAsync(SelectionProcessFilterDto filters)
{
_logger.LogInformation("Start getting filtered selection levels");

+ 19
- 0
Diligent.WebAPI.Contracts/DTOs/Pattern/PatternCreateDto.cs Zobrazit soubor

@@ -0,0 +1,19 @@
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; }
}
}

+ 22
- 0
Diligent.WebAPI.Contracts/DTOs/Pattern/PatternResponseDto.cs Zobrazit soubor

@@ -0,0 +1,22 @@
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; }
}
}

+ 19
- 0
Diligent.WebAPI.Contracts/DTOs/Pattern/PatternUpdateDto.cs Zobrazit soubor

@@ -0,0 +1,19 @@
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; }
}
}

+ 1
- 0
Diligent.WebAPI.Data/DatabaseContext.cs Zobrazit soubor

@@ -17,6 +17,7 @@ public class DatabaseContext : IdentityDbContext<User, AppRole, int>
public DbSet<Comment> Comments { get; set; }
public DbSet<SelectionLevel> SelectionLevels { get; set; }
public DbSet<SelectionProcess> SelectionProcesses { get; set; }
public DbSet<Pattern> Patterns { get; set; }


public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { }

+ 25
- 0
Diligent.WebAPI.Data/Entities/Pattern.cs Zobrazit soubor

@@ -0,0 +1,25 @@
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; }
}
}

+ 1062
- 0
Diligent.WebAPI.Data/Migrations/20221125142733_AddedPattern.Designer.cs
Diff nebyl zobrazen, protože je příliš veliký
Zobrazit soubor


+ 46
- 0
Diligent.WebAPI.Data/Migrations/20221125142733_AddedPattern.cs Zobrazit soubor

@@ -0,0 +1,46 @@
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");
}
}
}

+ 40
- 0
Diligent.WebAPI.Data/Migrations/DatabaseContextModelSnapshot.cs Zobrazit soubor

@@ -379,6 +379,35 @@ namespace Diligent.WebAPI.Data.Migrations
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 =>
{
b.Property<int>("Id")
@@ -868,6 +897,17 @@ namespace Diligent.WebAPI.Data.Migrations
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 =>
{
b.HasOne("Diligent.WebAPI.Data.Entities.User", "User")

+ 46
- 0
Diligent.WebAPI.Host/Controllers/V1/PatternsController.cs Zobrazit soubor

@@ -0,0 +1,46 @@
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();
}
}
}

+ 1
- 0
Diligent.WebAPI.Host/Extensions/BusinessConfigurationExtension.cs Zobrazit soubor

@@ -32,6 +32,7 @@
services.AddScoped<IAdService, AdService>();
services.AddScoped<ITechnologyService, TechnologyService>();
services.AddScoped<ICommentService, CommentService>();
services.AddScoped<IPatternService, PatternService>();
services.AddScoped<IScheduleService, ScheduleService>();
}


Načítá se…
Zrušit
Uložit