Skip to content

Commit

Permalink
update in Job posting.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammed-saalim committed Sep 19, 2024
1 parent da18f77 commit 333edd2
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 9 deletions.
3 changes: 2 additions & 1 deletion jobs/Endpoints/JobEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public static class JobEndpoints
public static void MapJobEndpoints(this IEndpointRouteBuilder routes)
{
routes.MapGet("/jobs", async (JobRequestHandler handler) => await handler.GetJobsAsync());
routes.MapPost("/jobs", async (JobCategoryDto jobDto, JobRequestHandler handler) => await handler.AddJobAsync(jobDto));
routes.MapPost("/jobCategory", async (JobCategoryDto jobDto, JobRequestHandler handler) => await handler.AddJobCategoryAsync(jobDto));
routes.MapPost("/jobs", async (JobDto jobDto, JobRequestHandler handler) => await handler.AddJobAsync(jobDto));
routes.MapGet("/", () => "Test Endpoint");
}
}
Expand Down
15 changes: 15 additions & 0 deletions jobs/Mappers/JobProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,25 @@ public JobProfile()
{
CreateMap<JobCategoryDto, JobCategory>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
.ForMember(dest => dest.Skills, opt => opt.MapFrom(src => src.Skills))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description));
CreateMap<JobCategory, JobCategoryDto>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description));

CreateMap<JobDto, Job>()
.ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
// .ForMember(dest => dest.Category, opt => opt.MapFrom(src => src.Category))
.ForMember(dest => dest.Price, opt => opt.MapFrom(src => src.Price))
.ForMember(dest => dest.DatePosted, opt => opt.MapFrom(src => src.DatePosted))
.ForMember(dest => dest.Zipcode, opt => opt.MapFrom(src => src.Zipcode));
CreateMap<Job, JobDto>()
.ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
.ForMember(dest => dest.Zipcode, opt => opt.MapFrom(src => src.Zipcode))
.ForMember(dest => dest.Price, opt => opt.MapFrom(src => src.Price));
// .ForMember(dest => dest.Category, opt => opt.MapFrom(src => src.Category));
}
}
}
45 changes: 45 additions & 0 deletions jobs/Messaging/RabbitMQ/EventListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Text.Json;
using localhands.Jobs.Models;


public class JobConsumer
{
private readonly string _hostname = "localhost"; // RabbitMQ host
private readonly string _queueName = "job_queue"; // Queue to receive jobs

public void ConsumeJobs()
{
var factory = new ConnectionFactory() { HostName = _hostname };

using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: _queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);

var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
var job = JsonSerializer.Deserialize<Job>(message);
ProcessJob(job);

Check warning on line 28 in jobs/Messaging/RabbitMQ/EventListener.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Possible null reference argument for parameter 'job' in 'void JobConsumer.ProcessJob(Job job)'.
};

channel.BasicConsume(queue: _queueName, autoAck: true, consumer: consumer);

Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}

private void ProcessJob(Job job)
{
// Save the job for workers or notify workers
// dbContext.WorkerJobs.Add(job);
// dbContext.SaveChanges();
Console.WriteLine("Processed Job: " + job.Title);
}
}
26 changes: 26 additions & 0 deletions jobs/Messaging/RabbitMQ/EventProducer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using RabbitMQ.Client;
using System.Text;
using System.Text.Json;
using localhands.Jobs.Models;

public class JobProducer
{
private readonly string _hostname = "localhost"; // RabbitMQ host
private readonly string _queueName = "job_queue"; // Queue to send jobs

public void SendJob(Job job)
{
var factory = new ConnectionFactory() { HostName = _hostname };

using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: _queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);

var message = JsonSerializer.Serialize(job);
var body = Encoding.UTF8.GetBytes(message);

channel.BasicPublish(exchange: "", routingKey: _queueName, basicProperties: null, body: body);
}
}
}
1 change: 1 addition & 0 deletions jobs/Models/Dto/JobCategoryDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class JobCategoryDto
{
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public List<string>? Skills { get; set; }

}
}
8 changes: 6 additions & 2 deletions jobs/Models/Dto/JobDto.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using localhands.Jobs.Models;

namespace localhands.Jobs.DTOs
{
public class JobDto
{
public string Id { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string JobCategoryDto { get; set; } = string.Empty;
public DateTime DatePosted { get; set; } = DateTime.Now;
// public JobCategoryDto Category { get; set; } = new JobCategoryDto();
public decimal Price { get; set; } = 0;
public JobPosterDto Poster { get; set; } = new JobPosterDto();
public string Zipcode { get; set; }

Check warning on line 13 in jobs/Models/Dto/JobDto.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Non-nullable property 'Zipcode' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

}

}
5 changes: 3 additions & 2 deletions jobs/Models/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public class Job
public string Id { get; set; }

Check warning on line 11 in jobs/Models/Job.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string Title { get; set; }

Check warning on line 12 in jobs/Models/Job.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Non-nullable property 'Title' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string Description { get; set; }

Check warning on line 13 in jobs/Models/Job.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Non-nullable property 'Description' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public JobCategory Category { get; set; }
// public JobCategory Category { get; set; }
public DateTime DatePosted { get; set; }
public bool IsFilled { get; set; }
public decimal Price { get; set; }
public JobPoster Poster { get; set; }
public required string Zipcode { get; set; }
}

public class JobCategory
Expand All @@ -25,6 +25,7 @@ public class JobCategory
public string Id { get; set; } = string.Empty;
public string? Name { get; set; }
public string? Description { get; set; }
public List<string>? Skills { get; set; }
}

public class JobPoster
Expand Down
10 changes: 8 additions & 2 deletions jobs/RequestHandler/JobRequesthandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ public async Task<IResult> GetJobsAsync()
}


public async Task<IResult> AddJobAsync(JobCategoryDto jobDto)
public async Task<IResult> AddJobCategoryAsync(JobCategoryDto jobDto)
{
await _jobService.AddJobAsync(jobDto);
await _jobService.AddJobCategoryAsync(jobDto);
return Results.Created($"/jobs/{jobDto.Name}", jobDto);
}

public async Task<IResult> AddJobAsync(JobDto jobDto)
{
await _jobService.AddJobAsync(jobDto);
return Results.Created($"/jobs/{jobDto.Title}", jobDto);
}
}
}
3 changes: 2 additions & 1 deletion jobs/ServiceProvider/Interface/IJobServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace localhands.Jobs.ServiceProvider.Interface{
public interface IJobsServiceProvider
{
Task<IEnumerable<JobCategoryDto>> GetJobsAsync();
Task AddJobAsync(JobCategoryDto jobDto);
Task AddJobAsync(JobDto jobDto);
Task AddJobCategoryAsync(JobCategoryDto jobDto);
}


Expand Down
11 changes: 10 additions & 1 deletion jobs/ServiceProvider/JobServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ namespace localhands.Jobs.ServiceProvider
public class JobsServiceProvider : IJobsServiceProvider
{
private readonly IMongoCollection<JobCategory> _jobCategory;
private readonly IMongoCollection<Job> _job;
private readonly IMapper _mapper;

public JobsServiceProvider(DbServiceProvider context, IMapper mapper)
{
_jobCategory = context.JobCategory;
_job = context.Jobs;
_mapper = mapper;
}

Expand All @@ -26,11 +28,18 @@ public async Task<IEnumerable<JobCategoryDto>> GetJobsAsync()
return _mapper.Map<IEnumerable<JobCategoryDto>>(jobs);
}

public async Task AddJobAsync(JobCategoryDto jobDto)
public async Task AddJobCategoryAsync(JobCategoryDto jobDto)
{
var job = _mapper.Map<JobCategory>(jobDto);
await _jobCategory.InsertOneAsync(job);

}


public async Task AddJobAsync(JobDto jobDto)
{
var job = _mapper.Map<Job>(jobDto);
await _job.InsertOneAsync(job);
}
}
}
5 changes: 5 additions & 0 deletions jobs/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"ConnectionString": "mongodb+srv://mohammed-saalim:[email protected]/?retryWrites=true&w=majority&appName=localhandscluster",
"DatabaseName": "your_database_name"
},
"RabbitMQ": {
"HostName": "localhost",
"UserName": "guest",
"Password": "guest"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
1 change: 1 addition & 0 deletions jobs/jobs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.5" />
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

Expand Down

0 comments on commit 333edd2

Please sign in to comment.