| @@ -9,6 +9,10 @@ namespace Diligent.WebAPI.Business.MappingProfiles | |||
| #region Models to DTO | |||
| CreateMap<Comment, CommentViewDto>(); | |||
| #endregion | |||
| #region DTO to Model | |||
| CreateMap<CommentCreateDto, Comment>(); | |||
| #endregion | |||
| } | |||
| } | |||
| } | |||
| @@ -7,7 +7,8 @@ namespace Diligent.WebAPI.Business.MappingProfiles | |||
| { | |||
| #region Model to DTO | |||
| CreateMap<Technology, TechnologyResponseDto>(); | |||
| CreateMap<TechnologyApplicant, TechnologyResponseDto>(); | |||
| //CreateMap<TechnologyApplicant, TechnologyResponseDto>(); -- ermin | |||
| CreateMap<TechnologyApplicant, TechnologyViewDto>(); | |||
| #endregion | |||
| } | |||
| } | |||
| @@ -15,17 +15,20 @@ namespace Diligent.WebAPI.Business.Services | |||
| public async Task<List<ApplicantViewDto>> GetAll() | |||
| { | |||
| var applicants = await _context.Applicants.ToListAsync(); | |||
| var applicants = await _context.Applicants.Include(c => c.Ads).ToListAsync(); | |||
| return _mapper.Map<List<ApplicantViewDto>>(applicants); | |||
| } | |||
| public async Task<ApplicantViewDto> GetById(int id) | |||
| { | |||
| var applicant = await _context.Applicants | |||
| .Include(x => x.Ads) | |||
| .ThenInclude(x => x.Technologies) | |||
| .Include(x => x.TechnologyApplicants) | |||
| .ThenInclude(x => x.Technology) | |||
| .Include(x => x.Comments) | |||
| .ThenInclude(t => t.User) | |||
| .FirstOrDefaultAsync(a => a.ApplicantId == id); | |||
| .FirstOrDefaultAsync(x => x.ApplicantId == id); | |||
| if (applicant is null) | |||
| throw new EntityNotFoundException("Applicant not found"); | |||
| @@ -0,0 +1,26 @@ | |||
| using Diligent.WebAPI.Contracts.DTOs.Comment; | |||
| namespace Diligent.WebAPI.Business.Services | |||
| { | |||
| public class CommentService : ICommentService | |||
| { | |||
| private readonly DatabaseContext _context; | |||
| private readonly IMapper _mapper; | |||
| public CommentService(DatabaseContext context, IMapper mapper) | |||
| { | |||
| _context = context; | |||
| _mapper = mapper; | |||
| } | |||
| public async Task CreateComment(CommentCreateDto commentCreateDto) | |||
| { | |||
| var comment = _mapper.Map<Comment>(commentCreateDto); | |||
| comment.DateOfSending = DateTime.Now; | |||
| await _context.Comments.AddAsync(comment); | |||
| await _context.SaveChangesAsync(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,9 @@ | |||
| using Diligent.WebAPI.Contracts.DTOs.Comment; | |||
| namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| { | |||
| public interface ICommentService | |||
| { | |||
| Task CreateComment(CommentCreateDto commentCreateDto); | |||
| } | |||
| } | |||
| @@ -4,7 +4,9 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| public interface ITechnologyService | |||
| { | |||
| Task<List<TechnologyResponseDto>> GetAllAsync(); | |||
| Task<List<TechnologyApplicantViewDto>> GetAllAsync2(); | |||
| Task<TechnologyResponseDto> GetByIdAsync(int id); | |||
| Task<TechnologyApplicantViewDto> GetByIdAsync2(int id); | |||
| } | |||
| } | |||
| @@ -20,6 +20,10 @@ namespace Diligent.WebAPI.Business.Services | |||
| public async Task<List<TechnologyResponseDto>> GetAllAsync() => | |||
| _mapper.Map<List<TechnologyResponseDto>>(await _context.Technologies.ToListAsync()); | |||
| public async Task<List<TechnologyApplicantViewDto>> GetAllAsync2() => | |||
| _mapper.Map<List<TechnologyApplicantViewDto>>(await _context.Technologies.ToListAsync()); | |||
| public async Task<TechnologyResponseDto> GetByIdAsync(int id) | |||
| { | |||
| var technology = await _context.Technologies.FindAsync(id); | |||
| @@ -29,5 +33,15 @@ namespace Diligent.WebAPI.Business.Services | |||
| return _mapper.Map<TechnologyResponseDto>(technology); | |||
| } | |||
| public async Task<TechnologyApplicantViewDto> GetByIdAsync2(int id) | |||
| { | |||
| var technology = await _context.Technologies.FindAsync(id); | |||
| if (technology is null) | |||
| throw new EntityNotFoundException("Technology not found"); | |||
| return _mapper.Map<TechnologyApplicantViewDto>(technology); | |||
| } | |||
| } | |||
| } | |||
| @@ -34,7 +34,7 @@ namespace Diligent.WebAPI.Contracts.DTOs.Ad | |||
| private int CalculateTotalApplicants() | |||
| { | |||
| return Applicants.Count(); | |||
| return Applicants.Count; | |||
| } | |||
| } | |||
| } | |||
| @@ -1,4 +1,5 @@ | |||
| using Diligent.WebAPI.Contracts.DTOs.Comment; | |||
| using Diligent.WebAPI.Contracts.DTOs.Ad; | |||
| using Diligent.WebAPI.Contracts.DTOs.Comment; | |||
| using Diligent.WebAPI.Contracts.DTOs.Technology; | |||
| namespace Diligent.WebAPI.Contracts.DTOs.Applicant | |||
| @@ -18,7 +19,8 @@ namespace Diligent.WebAPI.Contracts.DTOs.Applicant | |||
| public string BitBucketLink { get; set; } | |||
| public int Experience { get; set; } | |||
| public string ApplicationChannel { get; set; } | |||
| public List<TechnologyResponseDto> TechnologyApplicants { get; set; } = new(); | |||
| public List<TechnologyViewDto> TechnologyApplicants { get; set; } = new(); | |||
| public List<CommentViewDto> Comments { get; set; } | |||
| public List<AdResponseDto> Ads { get; set; } | |||
| } | |||
| } | |||
| @@ -0,0 +1,9 @@ | |||
| namespace Diligent.WebAPI.Contracts.DTOs.Comment | |||
| { | |||
| public class CommentCreateDto | |||
| { | |||
| public string Content { get; set; } | |||
| public int UserId { get; set; } | |||
| public int ApplicantId { get; set; } | |||
| } | |||
| } | |||
| @@ -4,7 +4,6 @@ namespace Diligent.WebAPI.Contracts.DTOs.Comment | |||
| { | |||
| public class CommentViewDto | |||
| { | |||
| public int Id { get; set; } | |||
| public string Content { get; set; } | |||
| public DateTime DateOfSending { get; set; } | |||
| public UserResponseDTO User { get; set; } | |||
| @@ -0,0 +1,7 @@ | |||
| namespace Diligent.WebAPI.Contracts.DTOs.Technology | |||
| { | |||
| public class TechnologyApplicantViewDto | |||
| { | |||
| public TechnologyResponseDto Technology { get; set; } | |||
| } | |||
| } | |||
| @@ -9,7 +9,6 @@ namespace Diligent.WebAPI.Contracts.DTOs.Technology | |||
| public class TechnologyResponseDto | |||
| { | |||
| public int TechnologyId { get; set; } | |||
| public string Name { get; set; } | |||
| } | |||
| } | |||
| @@ -0,0 +1,13 @@ | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace Diligent.WebAPI.Contracts.DTOs.Technology | |||
| { | |||
| public class TechnologyViewDto | |||
| { | |||
| public TechnologyResponseDto Technology { get; set; } | |||
| } | |||
| } | |||
| @@ -12,14 +12,14 @@ | |||
| _applicantService = applicantService; | |||
| } | |||
| [Authorize] | |||
| //[Authorize] | |||
| [HttpGet] | |||
| public async Task<IActionResult> GetAll() | |||
| { | |||
| return Ok(await _applicantService.GetAll()); | |||
| } | |||
| [Authorize] | |||
| //[Authorize] | |||
| [HttpGet("{id}")] | |||
| public async Task<IActionResult> GetById(int id) | |||
| { | |||
| @@ -0,0 +1,24 @@ | |||
| using Diligent.WebAPI.Contracts.DTOs.Comment; | |||
| namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| { | |||
| [ApiVersion("1.0")] | |||
| [Route("v{version:apiVersion}/comments")] | |||
| [ApiController] | |||
| public class CommentsController : ControllerBase | |||
| { | |||
| private readonly ICommentService _commentService; | |||
| public CommentsController(ICommentService commentService) | |||
| { | |||
| _commentService = commentService; | |||
| } | |||
| [Authorize] | |||
| [HttpPost] | |||
| public async Task<IActionResult> AddComment(CommentCreateDto commentCreateDto) | |||
| { | |||
| await _commentService.CreateComment(commentCreateDto); | |||
| return StatusCode((int)HttpStatusCode.Created); | |||
| } | |||
| } | |||
| } | |||
| @@ -27,6 +27,7 @@ | |||
| services.AddScoped<IAuthenticationService, AuthenticationService>(); | |||
| services.AddScoped<IAdService, AdService>(); | |||
| services.AddScoped<ITechnologyService, TechnologyService>(); | |||
| services.AddScoped<ICommentService, CommentService>(); | |||
| } | |||
| /// <summary> | |||
| @@ -4,9 +4,9 @@ using Microsoft.EntityFrameworkCore; | |||
| namespace Diligent.WebAPI.Tests | |||
| { | |||
| public static class Helpers | |||
| public static class Helpers<T> where T : class | |||
| { | |||
| public static async Task<DatabaseContext> GetDatabaseContext(List<Applicant> applicants) | |||
| public static async Task<DatabaseContext> GetDatabaseContext(List<T> applicants) | |||
| { | |||
| var options = new DbContextOptionsBuilder<DatabaseContext>() | |||
| .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) | |||
| @@ -15,7 +15,7 @@ namespace Diligent.WebAPI.Tests | |||
| databaseContext.Database.EnsureCreated(); | |||
| if (!await databaseContext.Applicants.AnyAsync()) | |||
| { | |||
| await databaseContext.Applicants.AddRangeAsync(applicants); | |||
| await databaseContext.Set<T>().AddRangeAsync(applicants); | |||
| await databaseContext.SaveChangesAsync(); | |||
| } | |||
| return databaseContext; | |||
| @@ -47,7 +47,7 @@ namespace Diligent.WebAPI.Tests.Services | |||
| [Fact] | |||
| public async Task GetAll_ShouldReturnListOfApplicants_Always() | |||
| { | |||
| var databaseContext = await Helpers.GetDatabaseContext(_applicants); | |||
| var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); | |||
| ApplicantService applicantService = new(databaseContext, _mapper); | |||
| var result = await applicantService.GetAll(); | |||
| @@ -58,7 +58,7 @@ namespace Diligent.WebAPI.Tests.Services | |||
| [Fact] | |||
| public async Task GetById_ShouldReturnApplicant_WhenApplicantExist() | |||
| { | |||
| var databaseContext = await Helpers.GetDatabaseContext(_applicants); | |||
| var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); | |||
| ApplicantService applicantService = new(databaseContext, _mapper); | |||
| var result = await applicantService.GetById(1); | |||
| @@ -69,7 +69,7 @@ namespace Diligent.WebAPI.Tests.Services | |||
| [Fact] | |||
| public async Task GetById_ShouldThrowEntityNotFooundException_WhenApplicantDontExist() | |||
| { | |||
| var databaseContext = await Helpers.GetDatabaseContext(_applicants); | |||
| var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); | |||
| ApplicantService applicantService = new(databaseContext, _mapper); | |||
| await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.GetById(1000)); | |||
| @@ -78,7 +78,7 @@ namespace Diligent.WebAPI.Tests.Services | |||
| [Fact] | |||
| public async Task CreateApplicant_ShouldAddEntityIntoDatabase_Always() | |||
| { | |||
| var databaseContext = await Helpers.GetDatabaseContext(_applicants); | |||
| var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); | |||
| ApplicantService applicantService = new(databaseContext, _mapper); | |||
| ApplicantCreateDto applicantCreateDto = new() | |||
| @@ -106,7 +106,7 @@ namespace Diligent.WebAPI.Tests.Services | |||
| [Fact] | |||
| public async Task DeleteApplicant_ShouldDeleteApplicant_WhenApplicantExist() | |||
| { | |||
| var databaseContext = await Helpers.GetDatabaseContext(_applicants); | |||
| var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); | |||
| ApplicantService applicantService = new(databaseContext, _mapper); | |||
| await applicantService.DeleteApplicant(1); | |||
| @@ -118,7 +118,7 @@ namespace Diligent.WebAPI.Tests.Services | |||
| [Fact] | |||
| public async Task DeleteApplicant_ShouldThrowEntityNotFooundException_WhenApplicantDontExist() | |||
| { | |||
| var databaseContext = await Helpers.GetDatabaseContext(_applicants); | |||
| var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); | |||
| ApplicantService applicantService = new(databaseContext, _mapper); | |||
| await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.DeleteApplicant(1000)); | |||
| @@ -127,7 +127,7 @@ namespace Diligent.WebAPI.Tests.Services | |||
| [Fact] | |||
| public async Task UpdateApplicant_ShouldUpdateApplicant_WhenApplicantExist() | |||
| { | |||
| var databaseContext = await Helpers.GetDatabaseContext(_applicants); | |||
| var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); | |||
| ApplicantService applicantService = new(databaseContext, _mapper); | |||
| ApplicantUpdateDto applicantUpdateDto = new() | |||