Profile save/load works
Still need to add skills, but #2 is getting there...
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
using JobsJobsJobs.Server.Data;
|
||||
using JobsJobsJobs.Shared;
|
||||
using JobsJobsJobs.Shared.Api;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NodaTime;
|
||||
using Npgsql;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -17,6 +19,7 @@ namespace JobsJobsJobs.Server.Areas.Api.Controllers
|
||||
/// API controller for employment profile information
|
||||
/// </summary>
|
||||
[Route("api/[controller]")]
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
public class ProfileController : ControllerBase
|
||||
{
|
||||
@@ -25,16 +28,21 @@ namespace JobsJobsJobs.Server.Areas.Api.Controllers
|
||||
/// </summary>
|
||||
private readonly NpgsqlConnection _db;
|
||||
|
||||
/// <summary>
|
||||
/// The NodaTime clock instance
|
||||
/// </summary>
|
||||
private readonly IClock _clock;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="db">The database connection to use for this request</param>
|
||||
public ProfileController(NpgsqlConnection db)
|
||||
public ProfileController(NpgsqlConnection db, IClock clock)
|
||||
{
|
||||
_db = db;
|
||||
_clock = clock;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("")]
|
||||
public async Task<IActionResult> Get()
|
||||
{
|
||||
@@ -43,5 +51,32 @@ namespace JobsJobsJobs.Server.Areas.Api.Controllers
|
||||
CitizenId.Parse(User.FindFirst(ClaimTypes.NameIdentifier)!.Value));
|
||||
return profile == null ? NoContent() : Ok(profile);
|
||||
}
|
||||
|
||||
[HttpPost("save")]
|
||||
public async Task<IActionResult> Save([FromBody] ProfileForm form)
|
||||
{
|
||||
var citizenId = CitizenId.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));
|
||||
await _db.OpenAsync();
|
||||
var existing = await _db.FindProfileByCitizen(citizenId);
|
||||
var profile = existing == null
|
||||
? new Profile(citizenId, form.IsSeekingEmployment, form.IsPublic, ContinentId.Parse(form.ContinentId),
|
||||
form.Region, form.RemoteWork, form.FullTime, new MarkdownString(form.Biography),
|
||||
_clock.GetCurrentInstant(),
|
||||
string.IsNullOrEmpty(form.Experience) ? null : new MarkdownString(form.Experience))
|
||||
: existing with
|
||||
{
|
||||
SeekingEmployment = form.IsSeekingEmployment,
|
||||
IsPublic = form.IsPublic,
|
||||
ContinentId = ContinentId.Parse(form.ContinentId),
|
||||
Region = form.Region,
|
||||
RemoteWork = form.RemoteWork,
|
||||
FullTime = form.FullTime,
|
||||
Biography = new MarkdownString(form.Biography),
|
||||
LastUpdatedOn = _clock.GetCurrentInstant(),
|
||||
Experience = string.IsNullOrEmpty(form.Experience) ? null : new MarkdownString(form.Experience)
|
||||
};
|
||||
await _db.SaveProfile(profile);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace JobsJobsJobs.Server.Data
|
||||
private static Profile ToProfile(NpgsqlDataReader rdr)
|
||||
{
|
||||
var continentId = ContinentId.Parse(rdr.GetString("continent_id"));
|
||||
return new Profile(CitizenId.Parse(rdr.GetString("id")), rdr.GetBoolean("seeking_employment"),
|
||||
return new Profile(CitizenId.Parse(rdr.GetString("citizen_id")), rdr.GetBoolean("seeking_employment"),
|
||||
rdr.GetBoolean("is_public"), continentId, rdr.GetString("region"), rdr.GetBoolean("remote_work"),
|
||||
rdr.GetBoolean("full_time"), new MarkdownString(rdr.GetString("biography")),
|
||||
rdr.GetInstant("last_updated_on"),
|
||||
@@ -48,5 +48,45 @@ namespace JobsJobsJobs.Server.Data
|
||||
using var rdr = await cmd.ExecuteReaderAsync().ConfigureAwait(false);
|
||||
return await rdr.ReadAsync().ConfigureAwait(false) ? ToProfile(rdr) : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save a profile
|
||||
/// </summary>
|
||||
/// <param name="profile">The profile to be saved</param>
|
||||
public static async Task SaveProfile(this NpgsqlConnection conn, Profile profile)
|
||||
{
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText =
|
||||
@"INSERT INTO profile (
|
||||
citizen_id, seeking_employment, is_public, continent_id, region, remote_work, full_time,
|
||||
biography, last_updated_on, experience
|
||||
) VALUES (
|
||||
@citizen_id, @seeking_employment, @is_public, @continent_id, @region, @remote_work, @full_time,
|
||||
@biography, @last_updated_on, @experience
|
||||
) ON CONFLICT (citizen_id) DO UPDATE
|
||||
SET seeking_employment = @seeking_employment,
|
||||
is_public = @is_public,
|
||||
continent_id = @continent_id,
|
||||
region = @region,
|
||||
remote_work = @remote_work,
|
||||
full_time = @full_time,
|
||||
biography = @biography,
|
||||
last_updated_on = @last_updated_on,
|
||||
experience = @experience
|
||||
WHERE profile.citizen_id = excluded.citizen_id";
|
||||
cmd.Parameters.Add(new NpgsqlParameter("@citizen_id", profile.Id.ToString()));
|
||||
cmd.Parameters.Add(new NpgsqlParameter("@seeking_employment", profile.SeekingEmployment));
|
||||
cmd.Parameters.Add(new NpgsqlParameter("@is_public", profile.IsPublic));
|
||||
cmd.Parameters.Add(new NpgsqlParameter("@continent_id", profile.ContinentId.ToString()));
|
||||
cmd.Parameters.Add(new NpgsqlParameter("@region", profile.Region));
|
||||
cmd.Parameters.Add(new NpgsqlParameter("@remote_work", profile.RemoteWork));
|
||||
cmd.Parameters.Add(new NpgsqlParameter("@full_time", profile.FullTime));
|
||||
cmd.Parameters.Add(new NpgsqlParameter("@biography", profile.Biography.Text));
|
||||
cmd.Parameters.Add(new NpgsqlParameter("@last_updated_on", profile.LastUpdatedOn));
|
||||
cmd.Parameters.Add(new NpgsqlParameter("@experience",
|
||||
profile.Experience == null ? DBNull.Value : profile.Experience.Text));
|
||||
|
||||
await cmd.ExecuteNonQueryAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user