Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jobs to db #561

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Streetcode/Streetcode.BLL/DTO/Jobs/JobChangeStatusDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Streetcode.BLL.DTO.Jobs
{
public class JobChangeStatusDto
{
public int Id { get; set; }
public bool Status { get; set; }
}
}
18 changes: 18 additions & 0 deletions Streetcode/Streetcode.BLL/DTO/Jobs/JobDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Streetcode.BLL.DTO.Jobs
{
public class JobDto
{
public int Id { get; set; }
public string Title { get; set; }
public bool Status { get; set; }
public string Description { get; set; }
public string Salary { get; set; }
}
}
16 changes: 16 additions & 0 deletions Streetcode/Streetcode.BLL/DTO/Jobs/JobShortDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Streetcode.BLL.DTO.Jobs
{
public class JobShortDto
{
public int Id { get; set; }
public string Title { get; set; }
public bool Status { get; set; }
public string Salary { get; set; }
}
}
15 changes: 15 additions & 0 deletions Streetcode/Streetcode.BLL/Mapping/Jobs/JobProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using AutoMapper;
using Streetcode.BLL.DTO.Jobs;
using Streetcode.DAL.Entities.Jobs;

namespace Streetcode.BLL.Mapping.Jobs
{
public class JobProfile : Profile
{
public JobProfile()
{
CreateMap<Job, JobDto>().ReverseMap();
CreateMap<Job, JobShortDto>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Jobs;

namespace Streetcode.BLL.MediatR.Jobs.ChangeStatus
{
public record ChangeJobStatusCommand(JobChangeStatusDto jobChangeStatusDto)
: IRequest<Result<int>>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using AutoMapper;
using FluentResults;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Repositories.Interfaces.Base;

namespace Streetcode.BLL.MediatR.Jobs.ChangeStatus
{
public class ChangeJobStatusHandler :
IRequestHandler<ChangeJobStatusCommand, Result<int>>
{
private readonly IMapper _mapper;
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ILoggerService _logger;

public ChangeJobStatusHandler(IMapper mapper, IRepositoryWrapper repositoryWrapper, ILoggerService logger)
{
_mapper = mapper;
_repositoryWrapper = repositoryWrapper;
_logger = logger;
}

public async Task<Result<int>> Handle(ChangeJobStatusCommand request, CancellationToken cancellationToken)
{
var job = await _repositoryWrapper.JobRepository.GetFirstOrDefaultAsync(j => j.Id == request.jobChangeStatusDto.Id);
if (job is null)
{
string exMessage = $"No job found by entered Id - {request.jobChangeStatusDto.Id}";
_logger.LogError(request, exMessage);
return Result.Fail(exMessage);
}

try
{
if (job.Status == request.jobChangeStatusDto.Status)
{
return Result.Ok(job.Id);
}

job.Status = request.jobChangeStatusDto.Status;
_repositoryWrapper.JobRepository.Update(job);
await _repositoryWrapper.SaveChangesAsync();
return Result.Ok(job.Id);
}
catch(Exception ex)
{
_logger.LogError(request, ex.Message);
return Result.Fail(ex.Message);
}
}
}
}
10 changes: 10 additions & 0 deletions Streetcode/Streetcode.BLL/MediatR/Jobs/Create/CreateJobCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Jobs;
using Streetcode.BLL.DTO.News;

namespace Streetcode.BLL.MediatR.Jobs.Create
{
public record CreateJobCommand(JobDto job)
: IRequest<Result<JobDto>>;
}
40 changes: 40 additions & 0 deletions Streetcode/Streetcode.BLL/MediatR/Jobs/Create/CreateJobHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using AutoMapper;
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Jobs;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Entities.Jobs;
using Streetcode.DAL.Repositories.Interfaces.Base;

namespace Streetcode.BLL.MediatR.Jobs.Create
{
public class CreateJobHandler : IRequestHandler<CreateJobCommand, Result<JobDto>>
{
private readonly IMapper _mapper;
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ILoggerService _logger;

public CreateJobHandler(IMapper mapper, IRepositoryWrapper repositoryWrapper, ILoggerService logger)
{
_mapper = mapper;
_repositoryWrapper = repositoryWrapper;
_logger = logger;
}

public async Task<Result<JobDto>> Handle(CreateJobCommand request, CancellationToken cancellationToken)
{
try
{
var job = _mapper.Map<Job>(request.job);
var createdJob = await _repositoryWrapper.JobRepository.CreateAsync(job);
await _repositoryWrapper.SaveChangesAsync();
return Result.Ok(_mapper.Map<JobDto>(createdJob));
}
catch (Exception ex)
{
_logger.LogError(request, ex.Message);
return Result.Fail(ex.Message);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using FluentResults;
using MediatR;

namespace Streetcode.BLL.MediatR.Jobs.Delete
{
public record DeleteJobCommand(int id)
: IRequest<Result<int>>;
}
43 changes: 43 additions & 0 deletions Streetcode/Streetcode.BLL/MediatR/Jobs/Delete/DeleteJobHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Repositories.Interfaces.Base;

namespace Streetcode.BLL.MediatR.Jobs.Delete
{
public class DeleteJobHandler : IRequestHandler<DeleteJobCommand, Result<int>>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly ILoggerService _logger;

public DeleteJobHandler(IRepositoryWrapper repository, ILoggerService logger)
{
_repositoryWrapper = repository;
_logger = logger;
}

public async Task<Result<int>> Handle(DeleteJobCommand request, CancellationToken cancellationToken)
{
var jobToDelete =
await _repositoryWrapper.JobRepository.GetFirstOrDefaultAsync(x => x.Id == request.id);
if (jobToDelete is null)
{
string exMessage = $"No job found by entered Id - {request.id}";
_logger.LogError(request, exMessage);
return Result.Fail(exMessage);
}

try
{
_repositoryWrapper.JobRepository.Delete(jobToDelete);
await _repositoryWrapper.SaveChangesAsync();
return Result.Ok(request.id);
}
catch(Exception ex)
{
_logger.LogError(request, ex.Message);
return Result.Fail(ex.Message);
}
}
}
}
32 changes: 32 additions & 0 deletions Streetcode/Streetcode.BLL/MediatR/Jobs/GetAll/GetAllJobsHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using AutoMapper;
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Jobs;
using Streetcode.BLL.Interfaces.BlobStorage;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Repositories.Interfaces.Base;

namespace Streetcode.BLL.MediatR.Jobs.GetAll
{
internal class GetAllJobsHandler
: IRequestHandler<GetAllJobsQuery, Result<IEnumerable<JobDto>>>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly IMapper _mapper;
private readonly ILoggerService _logger;

public GetAllJobsHandler(IRepositoryWrapper repository, IMapper mapper, ILoggerService logger)
{
_repositoryWrapper = repository;
_mapper = mapper;
_logger = logger;
}

public async Task<Result<IEnumerable<JobDto>>> Handle(GetAllJobsQuery request, CancellationToken cancellationToken)
{
var jobs = await _repositoryWrapper.JobRepository.GetAllAsync();
var jobsDto = _mapper.Map<IEnumerable<JobDto>>(jobs);
return Result.Ok(jobsDto);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Jobs;

namespace Streetcode.BLL.MediatR.Jobs.GetAll
{
public record GetAllJobsQuery()
: IRequest<Result<IEnumerable<JobDto>>>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using AutoMapper;
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Jobs;
using Streetcode.BLL.Interfaces.BlobStorage;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Repositories.Interfaces.Base;

namespace Streetcode.BLL.MediatR.Jobs.GetAll
{
internal class GetAllShortJobsHandler
: IRequestHandler<GetAllShortJobsQuery, Result<IEnumerable<JobShortDto>>>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly IMapper _mapper;
private readonly ILoggerService _logger;

public GetAllShortJobsHandler(IRepositoryWrapper repository, IMapper mapper, ILoggerService logger)
{
_repositoryWrapper = repository;
_mapper = mapper;
_logger = logger;
}

public async Task<Result<IEnumerable<JobShortDto>>> Handle(GetAllShortJobsQuery request, CancellationToken cancellationToken)
{
var jobs = await _repositoryWrapper.JobRepository.GetAllAsync();
var jobsDto = _mapper.Map<IEnumerable<JobShortDto>>(jobs);
return Result.Ok(jobsDto);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Jobs;

namespace Streetcode.BLL.MediatR.Jobs.GetAll
{
public record GetAllShortJobsQuery()
: IRequest<Result<IEnumerable<JobShortDto>>>;
}
10 changes: 10 additions & 0 deletions Streetcode/Streetcode.BLL/MediatR/Jobs/Update/UpdateJobCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Jobs;
using Streetcode.BLL.DTO.News;

namespace Streetcode.BLL.MediatR.Jobs.Update
{
public record UpdateJobCommand(JobDto job)
: IRequest<Result<JobDto>>;
}
49 changes: 49 additions & 0 deletions Streetcode/Streetcode.BLL/MediatR/Jobs/Update/UpdateJobHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using AutoMapper;
using FluentResults;
using MediatR;
using Streetcode.BLL.DTO.Jobs;
using Streetcode.BLL.Interfaces.BlobStorage;
using Streetcode.BLL.Interfaces.Logging;
using Streetcode.DAL.Entities.Jobs;
using Streetcode.DAL.Repositories.Interfaces.Base;

namespace Streetcode.BLL.MediatR.Jobs.Update
{
public class UpdateJobHandler : IRequestHandler<UpdateJobCommand, Result<JobDto>>
{
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly IMapper _mapper;
private readonly ILoggerService _logger;
public UpdateJobHandler(IRepositoryWrapper repository, IMapper mapper, ILoggerService logger)
{
_repositoryWrapper = repository;
_mapper = mapper;
_logger = logger;
}

public async Task<Result<JobDto>> Handle(UpdateJobCommand request, CancellationToken cancellationToken)
{
var existedJob =
await _repositoryWrapper.JobRepository.GetFirstOrDefaultAsync(x => x.Id == request.job.Id);
if (existedJob is null)
{
string exMessage = $"No job found by entered Id - {request.job.Id}";
_logger.LogError(request, exMessage);
return Result.Fail(exMessage);
}

try
{
var jobToUpdate = _mapper.Map<Job>(request.job);
_repositoryWrapper.JobRepository.Update(jobToUpdate);
await _repositoryWrapper.SaveChangesAsync();
return Result.Ok(_mapper.Map<JobDto>(jobToUpdate));
}
catch(Exception ex)
{
_logger.LogError(request, ex.Message);
return Result.Fail(ex.Message);
}
}
}
}
Loading
Loading