First Commit
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using JobTrackerApi.Data;
|
||||
using JobTrackerApi.Models;
|
||||
|
||||
namespace JobTrackerApi.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/correspondence")]
|
||||
public class CorrespondenceController : ControllerBase
|
||||
{
|
||||
private readonly JobTrackerContext _db;
|
||||
|
||||
public CorrespondenceController(JobTrackerContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
// GET all messages for a job
|
||||
[HttpGet("{jobId:int}")]
|
||||
public async Task<ActionResult<List<Correspondence>>> GetForJob([FromRoute] int jobId, CancellationToken cancellationToken)
|
||||
{
|
||||
var jobOk = await _db.JobApplications.AnyAsync(j => j.Id == jobId, cancellationToken);
|
||||
if (!jobOk) return NotFound();
|
||||
|
||||
var messages = await _db.Correspondences
|
||||
.Where(c => c.JobApplicationId == jobId)
|
||||
.OrderBy(c => c.Date)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return Ok(messages);
|
||||
}
|
||||
|
||||
public sealed record CreateCorrespondenceRequest(int JobApplicationId, string From, string Content);
|
||||
public sealed record CreateCorrespondenceRequestV2(
|
||||
int JobApplicationId,
|
||||
string From,
|
||||
string Content,
|
||||
string? Subject,
|
||||
string? Channel,
|
||||
DateTime? Date
|
||||
);
|
||||
|
||||
// POST new message
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<Correspondence>> Create([FromBody] CreateCorrespondenceRequestV2 request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.JobApplicationId <= 0) return BadRequest("Valid jobApplicationId is required.");
|
||||
if (string.IsNullOrWhiteSpace(request.From)) return BadRequest("From is required.");
|
||||
if (string.IsNullOrWhiteSpace(request.Content)) return BadRequest("Content is required.");
|
||||
|
||||
var exists = await _db.JobApplications.AnyAsync(j => j.Id == request.JobApplicationId, cancellationToken);
|
||||
if (!exists) return BadRequest("jobApplicationId does not exist.");
|
||||
|
||||
var message = new Correspondence
|
||||
{
|
||||
JobApplicationId = request.JobApplicationId,
|
||||
From = request.From.Trim(),
|
||||
Subject = string.IsNullOrWhiteSpace(request.Subject) ? null : request.Subject.Trim(),
|
||||
Channel = string.IsNullOrWhiteSpace(request.Channel) ? null : request.Channel.Trim(),
|
||||
Content = request.Content,
|
||||
Date = request.Date ?? DateTime.Now,
|
||||
};
|
||||
|
||||
_db.Correspondences.Add(message);
|
||||
await _db.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return CreatedAtAction(nameof(GetForJob), new { jobId = message.JobApplicationId }, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user