| #region Models to DTOs | #region Models to DTOs | ||||
| CreateMap<Applicant, ApplicantViewDto>(); | CreateMap<Applicant, ApplicantViewDto>(); | ||||
| CreateMap<Applicant, AdApplicantViewDto>(); | CreateMap<Applicant, AdApplicantViewDto>(); | ||||
| CreateMap<Applicant, ApplicantScheduleViewDto>(); | |||||
| #endregion | #endregion | ||||
| #region DTOs to Models | #region DTOs to Models |
| using Diligent.WebAPI.Contracts.DTOs.SelectionProcess; | |||||
| using Diligent.WebAPI.Contracts.DTOs.Schedule; | |||||
| using Diligent.WebAPI.Contracts.DTOs.SelectionProcess; | |||||
| namespace Diligent.WebAPI.Business.MappingProfiles | namespace Diligent.WebAPI.Business.MappingProfiles | ||||
| { | { | ||||
| #region Model to DTO | #region Model to DTO | ||||
| CreateMap<SelectionProcess, SelectionProcessResposneDto>(); | CreateMap<SelectionProcess, SelectionProcessResposneDto>(); | ||||
| CreateMap<SelectionProcess, SelectionProcessResposneWithoutApplicantDto>(); | CreateMap<SelectionProcess, SelectionProcessResposneWithoutApplicantDto>(); | ||||
| CreateMap<SelectionProcess, ScheduleViewDto>(); | |||||
| #endregion | #endregion | ||||
| } | } | ||||
| } | } |
| using Diligent.WebAPI.Contracts.DTOs.Schedule; | |||||
| namespace Diligent.WebAPI.Business.Services.Interfaces | |||||
| { | |||||
| public interface IScheduleService | |||||
| { | |||||
| Task<List<ScheduleViewDto>> GetScheduleForCertainPeriod(int month, int year); | |||||
| } | |||||
| } |
| using Diligent.WebAPI.Contracts.DTOs.Schedule; | |||||
| namespace Diligent.WebAPI.Business.Services | |||||
| { | |||||
| public class ScheduleService : IScheduleService | |||||
| { | |||||
| private readonly DatabaseContext _context; | |||||
| private readonly IMapper _mapper; | |||||
| private readonly ILogger<ScheduleService> _logger; | |||||
| public ScheduleService(DatabaseContext context, IMapper mapper,ILogger<ScheduleService> logger) | |||||
| { | |||||
| _context = context; | |||||
| _mapper = mapper; | |||||
| _logger = logger; | |||||
| } | |||||
| public async Task<List<ScheduleViewDto>> GetScheduleForCertainPeriod(int month, int year) | |||||
| { | |||||
| _logger.LogInformation("Start getting schedule for certain period"); | |||||
| _logger.LogInformation("Getting data from DB and filter"); | |||||
| var selectionProcessess = await _context.SelectionProcesses | |||||
| .Include(c => c.Applicant) | |||||
| .Include(c => c.SelectionLevel) | |||||
| .Where(k => k.Date != null && k.Date.Value.Month == month && k.Date.Value.Year == year) | |||||
| .ToListAsync(); | |||||
| _logger.LogInformation($"Got {selectionProcessess.Count} selection processes"); | |||||
| _logger.LogInformation($"Return schedule for certain period"); | |||||
| return _mapper.Map<List<ScheduleViewDto>>(selectionProcessess); | |||||
| } | |||||
| } | |||||
| } |
| namespace Diligent.WebAPI.Contracts.DTOs.Applicant | |||||
| { | |||||
| public class ApplicantScheduleViewDto | |||||
| { | |||||
| public string FirstName { get; set; } | |||||
| public string LastName { get; set; } | |||||
| } | |||||
| } |
| using Diligent.WebAPI.Contracts.DTOs.Applicant; | |||||
| using Diligent.WebAPI.Contracts.DTOs.SelectionLevel; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.Schedule | |||||
| { | |||||
| public class ScheduleViewDto | |||||
| { | |||||
| public DateTime? Date { get; set; } | |||||
| public string? Link { get; set; } | |||||
| public SelectionLevelResposneDto SelectionLevel { get; set; } | |||||
| public ApplicantScheduleViewDto Applicant { get; set; } | |||||
| } | |||||
| } |
| namespace Diligent.WebAPI.Host.Controllers.V1 | |||||
| { | |||||
| [ApiVersion("1.0")] | |||||
| [Route("v{version:apiVersion}/schedule")] | |||||
| [ApiController] | |||||
| public class ScheduleController:ControllerBase | |||||
| { | |||||
| private readonly IScheduleService _scheduleService; | |||||
| public ScheduleController(IScheduleService scheduleService) | |||||
| { | |||||
| _scheduleService = scheduleService; | |||||
| } | |||||
| [HttpGet] | |||||
| public async Task<IActionResult> GetSchedule(int month,int year) => | |||||
| Ok(await _scheduleService.GetScheduleForCertainPeriod(month, year)); | |||||
| } | |||||
| } |
| 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<IScheduleService, ScheduleService>(); | |||||
| } | } | ||||
| /// <summary> | /// <summary> |
| { | { | ||||
| public class ApplicantsControllerTests | public class ApplicantsControllerTests | ||||
| { | { | ||||
| private IApplicantService _applicantService = Substitute.For<IApplicantService>(); | |||||
| private readonly IApplicantService _applicantService = Substitute.For<IApplicantService>(); | |||||
| private readonly ApplicantViewDto _applicant; | private readonly ApplicantViewDto _applicant; | ||||
| public ApplicantsControllerTests() | public ApplicantsControllerTests() | ||||
| { | { |
| { | { | ||||
| public class CommentsControllerTests | public class CommentsControllerTests | ||||
| { | { | ||||
| private ICommentService _commentService = Substitute.For<ICommentService>(); | |||||
| public CommentsControllerTests() | |||||
| { | |||||
| } | |||||
| private readonly ICommentService _commentService = Substitute.For<ICommentService>(); | |||||
| [Fact] | [Fact] | ||||
| public async Task Addcomment_ShouldReturn_201Created_Always() | public async Task Addcomment_ShouldReturn_201Created_Always() | ||||
| { | { | ||||
| int count = 0; | |||||
| _commentService.When(x => x.CreateComment(Arg.Any<CommentCreateDto>())).Do(x => { count++; }); | |||||
| _commentService.When(x => x.CreateComment(Arg.Any<CommentCreateDto>())).Do(x => { }); | |||||
| CommentsController commentsController = new(_commentService); | CommentsController commentsController = new(_commentService); | ||||
| CommentCreateDto commentCreateDto = new() | CommentCreateDto commentCreateDto = new() | ||||
| ApplicantId = 1000, | ApplicantId = 1000, | ||||
| Content = "some text", | Content = "some text", | ||||
| UserId = 1000, | UserId = 1000, | ||||
| UsersToNotify = new List<string>() | |||||
| }; | }; | ||||
| var result = await commentsController.AddComment(commentCreateDto); | var result = await commentsController.AddComment(commentCreateDto); | ||||
| namespace Diligent.WebAPI.Tests.Controllers | |||||
| { | |||||
| public class ScheduleControllerTests | |||||
| { | |||||
| private IScheduleService _scheduleService = Substitute.For<IScheduleService>(); | |||||
| [Fact] | |||||
| public async Task GetSchedule_ShouldReturn_200OK_Always() | |||||
| { | |||||
| _scheduleService.When(x => x.GetScheduleForCertainPeriod(Arg.Any<int>(), Arg.Any<int>())).Do(x => { }); | |||||
| ScheduleController scheduleController = new(_scheduleService); | |||||
| var result = await scheduleController.GetSchedule(1000, 1000); | |||||
| (result as OkObjectResult).StatusCode.Should().Be(200); | |||||
| } | |||||
| } | |||||
| } |
| TypeOfEmployment = Applicant.TypesOfEmployment.Intership, | TypeOfEmployment = Applicant.TypesOfEmployment.Intership, | ||||
| SelectionProcesses = new List<SelectionProcess> | SelectionProcesses = new List<SelectionProcess> | ||||
| { | { | ||||
| new SelectionProcess{ Id = 1, Status = "", Name = ""}, | |||||
| new SelectionProcess{ Id = 2, Status = "", Name = ""}, | |||||
| new SelectionProcess{ Id = 3, Status = "", Name = ""} | |||||
| new SelectionProcess{ Status = "", Name = ""}, | |||||
| new SelectionProcess{ Status = "", Name = ""}, | |||||
| new SelectionProcess{ Status = "", Name = ""} | |||||
| } | } | ||||
| }; | }; | ||||
| var applicants = new List<Applicant> | var applicants = new List<Applicant> |
| using AutoMapper; | using AutoMapper; | ||||
| using Castle.Core.Logging; | |||||
| using Diligent.WebAPI.Business.Extensions; | |||||
| using Diligent.WebAPI.Business.MappingProfiles; | using Diligent.WebAPI.Business.MappingProfiles; | ||||
| using Diligent.WebAPI.Business.Services; | using Diligent.WebAPI.Business.Services; | ||||
| using Diligent.WebAPI.Contracts.DTOs.Ad; | using Diligent.WebAPI.Contracts.DTOs.Ad; | ||||
| using Diligent.WebAPI.Contracts.DTOs.Applicant; | using Diligent.WebAPI.Contracts.DTOs.Applicant; | ||||
| using Diligent.WebAPI.Contracts.Exceptions; | using Diligent.WebAPI.Contracts.Exceptions; | ||||
| using Diligent.WebAPI.Data.Entities; | using Diligent.WebAPI.Data.Entities; | ||||
| using Microsoft.AspNetCore.Http; | |||||
| using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||
| using static Diligent.WebAPI.Data.Entities.Applicant; | |||||
| namespace Diligent.WebAPI.Tests.Services | namespace Diligent.WebAPI.Tests.Services | ||||
| { | { | ||||
| { | { | ||||
| private readonly IMapper _mapper; | private readonly IMapper _mapper; | ||||
| private ILogger<ApplicantService> _logger = Substitute.For<ILogger<ApplicantService>>(); | private ILogger<ApplicantService> _logger = Substitute.For<ILogger<ApplicantService>>(); | ||||
| private readonly HttpContext _httpContext; | |||||
| private readonly List<Applicant> _applicants; | private readonly List<Applicant> _applicants; | ||||
| private readonly List<Ad> _ads; | private readonly List<Ad> _ads; | ||||
| public ApplicantServiceTests() | public ApplicantServiceTests() | ||||
| new SelectionProcessMappingProfile() | new SelectionProcessMappingProfile() | ||||
| })); | })); | ||||
| _mapper = new Mapper(configuration); | _mapper = new Mapper(configuration); | ||||
| _httpContext = new DefaultHttpContext(); | |||||
| } | } | ||||
| [Fact] | [Fact] |
| using AutoMapper; | |||||
| using Diligent.WebAPI.Business.MappingProfiles; | |||||
| using Diligent.WebAPI.Business.Services; | |||||
| using Diligent.WebAPI.Contracts.DTOs.Schedule; | |||||
| using Diligent.WebAPI.Data.Entities; | |||||
| using Microsoft.Extensions.Logging; | |||||
| namespace Diligent.WebAPI.Tests.Services | |||||
| { | |||||
| public class ScheduleServiceTests | |||||
| { | |||||
| private readonly IMapper _mapper; | |||||
| private ILogger<ScheduleService> _logger = Substitute.For<ILogger<ScheduleService>>(); | |||||
| public ScheduleServiceTests() | |||||
| { | |||||
| var configuration = new MapperConfiguration(cfg => cfg.AddProfiles( | |||||
| new List<Profile> | |||||
| { | |||||
| new ApplicantMappingProfile(), | |||||
| new SelectionProcessMappingProfile(), | |||||
| new SelectionLevelMappingProfile() | |||||
| })); | |||||
| _mapper = new Mapper(configuration); | |||||
| } | |||||
| [Fact] | |||||
| public async Task GetScheduleForCertainPeriod_ShouldReturnListOfSchedules_Always() | |||||
| { | |||||
| var selectionProcesses = MockData.GetListOfSelectionProcess(); | |||||
| var databaseContext = await Helpers<SelectionProcess>.GetDatabaseContext(selectionProcesses); | |||||
| ScheduleService scheduleService = new(databaseContext, _mapper, _logger); | |||||
| var result = await scheduleService.GetScheduleForCertainPeriod(DateTime.Now.Month, DateTime.Now.Year); | |||||
| result.Should().BeEquivalentTo(_mapper.Map<List<ScheduleViewDto>>(selectionProcesses)); | |||||
| } | |||||
| } | |||||
| } |