Add deletion options (#9)
This commit is contained in:
@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using NodaTime;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JobsJobsJobs.Server.Areas.Api.Controllers
|
||||
@@ -44,6 +45,11 @@ namespace JobsJobsJobs.Server.Areas.Api.Controllers
|
||||
_db = db;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The current citizen ID
|
||||
/// </summary>
|
||||
private CitizenId CurrentCitizenId => CitizenId.Parse(User.FindFirst(ClaimTypes.NameIdentifier)!.Value);
|
||||
|
||||
[HttpGet("log-on/{authCode}")]
|
||||
public async Task<IActionResult> LogOn([FromRoute] string authCode)
|
||||
{
|
||||
@@ -87,5 +93,15 @@ namespace JobsJobsJobs.Server.Areas.Api.Controllers
|
||||
var citizen = await _db.FindCitizenById(CitizenId.Parse(id));
|
||||
return citizen == null ? NotFound() : Ok(citizen);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpDelete("")]
|
||||
public async Task<IActionResult> Remove()
|
||||
{
|
||||
await _db.DeleteCitizen(CurrentCitizenId);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,6 +129,14 @@ namespace JobsJobsJobs.Server.Areas.Api.Controllers
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
|
||||
[HttpDelete("")]
|
||||
public async Task<IActionResult> Remove()
|
||||
{
|
||||
await _db.DeleteProfileByCitizen(CurrentCitizenId);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace JobsJobsJobs.Server.Data
|
||||
/// </summary>
|
||||
/// <param name="citizen">The citizen to be added</param>
|
||||
public static async Task AddCitizen(this JobsDbContext db, Citizen citizen) =>
|
||||
await db.Citizens.AddAsync(citizen);
|
||||
await db.Citizens.AddAsync(citizen).ConfigureAwait(false);
|
||||
|
||||
/// <summary>
|
||||
/// Update a citizen after they have logged on (update last seen, sync display name)
|
||||
@@ -42,5 +42,20 @@ namespace JobsJobsJobs.Server.Data
|
||||
/// <param name="citizen">The updated citizen</param>
|
||||
public static void UpdateCitizen(this JobsDbContext db, Citizen citizen) =>
|
||||
db.Entry(citizen).State = EntityState.Modified;
|
||||
|
||||
/// <summary>
|
||||
/// Delete a citizen
|
||||
/// </summary>
|
||||
/// <param name="citizenId">The ID of the citizen to be deleted</param>
|
||||
/// <returns></returns>
|
||||
public static async Task DeleteCitizen(this JobsDbContext db, CitizenId citizenId)
|
||||
{
|
||||
var id = citizenId.ToString();
|
||||
await db.DeleteProfileByCitizen(citizenId).ConfigureAwait(false);
|
||||
await db.Database.ExecuteSqlInterpolatedAsync($"DELETE FROM jjj.success WHERE citizen_id = {id}")
|
||||
.ConfigureAwait(false);
|
||||
await db.Database.ExecuteSqlInterpolatedAsync($"DELETE FROM jjj.citizen WHERE id = {id}")
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,5 +160,18 @@ namespace JobsJobsJobs.Server.Data
|
||||
x.Profile.SeekingEmployment, x.Profile.RemoteWork, x.Profile.FullTime, x.Profile.LastUpdatedOn))
|
||||
.ToListAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete skills and profile for the given citizen
|
||||
/// </summary>
|
||||
/// <param name="citizenId">The ID of the citizen whose profile should be deleted</param>
|
||||
public static async Task DeleteProfileByCitizen(this JobsDbContext db, CitizenId citizenId)
|
||||
{
|
||||
var id = citizenId.ToString();
|
||||
await db.Database.ExecuteSqlInterpolatedAsync($"DELETE FROM jjj.skill WHERE citizen_id = {id}")
|
||||
.ConfigureAwait(false);
|
||||
await db.Database.ExecuteSqlInterpolatedAsync($"DELETE FROM jjj.profile WHERE citizen_id = {id}")
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user