Skip to content

Commit

Permalink
feat: Add migration and build script; enhance seeker functionality
Browse files Browse the repository at this point in the history
- Implemented shell scripts for creating migrations and managing build lifecycle.
- Added seeker-specific modifications for streamlined functionality.
- Included dynamic migration naming with random suffix generation.
  • Loading branch information
nixhantb committed Jan 24, 2025
1 parent 2bf5ecf commit 7d55fcc
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 20 deletions.
48 changes: 48 additions & 0 deletions Server/JobLeet.WebApi/DBMigration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

random_number() {
echo $((RANDOM % 10000 + 1))
}

echo "Select the context for the migration:"
echo "1. ApplicationDbContext (AppDB)"
echo "2. BaseDbContext (BaseDB)"
read -p "Enter your choice (1 or 2): " choice

suffix=$(random_number)

# Handle user choice
case $choice in
1)
context="ApplicationDbContext"
migration_name="AppDBCreate$suffix"
;;
2)
context="BaseDBContext"
migration_name="BaseDBCreate$suffix"
;;
*)
echo "Invalid choice. Exiting."
exit 1
;;
esac

# Run the EF Core migration and update database
command="dotnet ef migrations add $migration_name --context $context"
echo "Running: $command"
$command

if [ $? -eq 0 ]; then
echo "Migration created successfully."
update_command="dotnet ef database update --context $context"
echo "Running: $update_command"
$update_command

if [ $? -eq 0 ]; then
echo "Database updated successfully."
else
echo "Failed to update the database."
fi
else
echo "Failed to create the migration."
fi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace JobLeet.WebApi.JobLeet.Api.Models.Seekers.V1
{
public class SeekerModel : BaseModel
{
public PersonNameModel personNameModel { get; set; }

Check warning on line 7 in Server/JobLeet.WebApi/JobLeet.Api/Models/Seekers/V1/SeekerModel.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'personNameModel' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public PhoneModel? Phone { get; set; }
public AddressModel? Address { get; set; }
public SkillModel? Skills { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
using JobLeet.WebApi.JobLeet.Core.Entities.Companies.V1;
using JobLeet.WebApi.JobLeet.Core.Entities.Employers.V1;
using JobLeet.WebApi.JobLeet.Core.Entities.Seekers.V1;

namespace JobLeet.WebApi.JobLeet.Core.Entities.Jobs.V1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace JobLeet.WebApi.JobLeet.Core.Entities.Seekers.V1
{
public class Seeker : BaseEntity
{
public PersonName PersonName { get; set; }
public Phone? Phone { get; set; }
public Address? Address { get; set; }
public Skill? Skills { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public static Seeker ToSeekerDataBase(Seeker entity, string UserId)
return new Seeker
{
Id = UserId,
PersonName = new()
{
Id = entity.PersonName.Id,
FirstName = entity.PersonName.FirstName,
MiddleName = entity.PersonName.MiddleName,
LastName = entity.PersonName.LastName,
},
Phone = PhoneMapper.ToPhoneDatabase(entity.Phone),
Address = AddressMapper.ToAddressDatabase(entity.Address),
Skills = SkillsMapper.ToSkillsDB(entity.Skills),
Expand Down Expand Up @@ -46,6 +53,13 @@ public static SeekerModel ToSeekerModel(Seeker model)
return new SeekerModel
{
Id = model.Id,
personNameModel = new()
{
Id = model.PersonName.Id,
FirstName = model.PersonName.FirstName,
MiddleName = model.PersonName.MiddleName,
LastName = model.PersonName.LastName,
},
Phone = model.Phone != null ? PhoneMapper.ToPhoneModel(model.Phone) : null,
Address =
model.Address != null ? AddressMapper.ToAddressModel(model.Address) : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,17 @@ public async Task<SeekerModel> AddAsync(Seeker entity)
throw new Exception("Unable to retrieve the logged-in user ID.");
}

// Check if the user exists in the AspNetUsers table
var user = await _authContext.Users.FirstOrDefaultAsync(e => e.Id == userId);
if (user == null)
{
throw new Exception("User not found in the identity database.");
}

// Map the Seeker entity to the database model, using the logged-in user's ID
var seekersEntity = SeekersMapper.ToSeekerDataBase(entity, userId);

// Add and save the Seekers entity to the database
await _dbContext.AddAsync(seekersEntity);
await _dbContext.SaveChangesAsync();

// Map the database entity back to the response model
var seekersResponse = SeekersMapper.ToSeekerModel(seekersEntity);
return seekersResponse;
}
Expand All @@ -75,6 +71,7 @@ public async Task<SeekerModel> GetByIdAsync(string id)
{
var seeker = await _dbContext
.Seekers.Where(c => c.Id.Equals(id))
.Include(p => p.PersonName)
.Include(c => c.Phone)
.Include(c => c.Address)
.Include(c => c.Skills)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public class SeekerValidator : AbstractValidator<Seeker>
{
public SeekerValidator()
{
RuleFor(seeker => seeker.PersonName)
.NotNull()
.WithMessage("Person Name is required.")
.When(seeker => seeker.Phone != null)
.SetValidator(new PersonNameValidator());
RuleFor(seeker => seeker.Phone)
.NotNull()
.WithMessage("Phone information is required.")
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#nullable disable

namespace JobLeet.WebApi.Migrations.ApplicationDb
namespace JobLeet.WebApi.Migrations
{
/// <inheritdoc />
public partial class Authentication : Migration
public partial class AppDBCreate1571 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#nullable disable

namespace JobLeet.WebApi.Migrations.ApplicationDb
namespace JobLeet.WebApi.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#nullable disable

namespace JobLeet.WebApi.Migrations
namespace JobLeet.WebApi.Migrations.BaseDB
{
/// <inheritdoc />
public partial class InitialCreate : Migration
public partial class BaseDBCreate9892 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
Expand Down Expand Up @@ -337,6 +337,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
columns: table => new
{
seekerid = table.Column<string>(name: "seeker_id", type: "text", nullable: false),
PersonNameId = table.Column<string>(type: "text", nullable: true),
PhoneId = table.Column<string>(type: "text", nullable: true),
AddressId = table.Column<string>(type: "text", nullable: true),
SkillsId = table.Column<string>(type: "text", nullable: true),
Expand Down Expand Up @@ -364,6 +365,11 @@ protected override void Up(MigrationBuilder migrationBuilder)
column: x => x.ExperienceId,
principalTable: "jblt_experience",
principalColumn: "experience_id");
table.ForeignKey(
name: "FK_jblt_seeker_jblt_personName_PersonNameId",
column: x => x.PersonNameId,
principalTable: "jblt_personName",
principalColumn: "personname_id");
table.ForeignKey(
name: "FK_jblt_seeker_jblt_phone_PhoneId",
column: x => x.PhoneId,
Expand Down Expand Up @@ -551,6 +557,11 @@ protected override void Up(MigrationBuilder migrationBuilder)
table: "jblt_seeker",
column: "ExperienceId");

migrationBuilder.CreateIndex(
name: "IX_jblt_seeker_PersonNameId",
table: "jblt_seeker",
column: "PersonNameId");

migrationBuilder.CreateIndex(
name: "IX_jblt_seeker_PhoneId",
table: "jblt_seeker",
Expand Down Expand Up @@ -588,15 +599,15 @@ protected override void Down(MigrationBuilder migrationBuilder)
migrationBuilder.DropTable(
name: "jblt_seeker");

migrationBuilder.DropTable(
name: "jblt_personName");

migrationBuilder.DropTable(
name: "jblt_education");

migrationBuilder.DropTable(
name: "jblt_experience");

migrationBuilder.DropTable(
name: "jblt_personName");

migrationBuilder.DropTable(
name: "jblt_qualification");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#nullable disable

namespace JobLeet.WebApi.Migrations
namespace JobLeet.WebApi.Migrations.BaseDB
{
[DbContext(typeof(BaseDBContext))]
partial class BaseDBContextModelSnapshot : ModelSnapshot
Expand Down Expand Up @@ -570,6 +570,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<string>("LinkedInProfile")
.HasColumnType("text");

b.Property<string>("PersonNameId")
.HasColumnType("text");

b.Property<string>("PhoneId")
.HasColumnType("text");

Expand All @@ -593,6 +596,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)

b.HasIndex("ExperienceId");

b.HasIndex("PersonNameId");

b.HasIndex("PhoneId");

b.HasIndex("QualificationsId");
Expand Down Expand Up @@ -766,6 +771,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.WithMany()
.HasForeignKey("ExperienceId");

b.HasOne("JobLeet.WebApi.JobLeet.Core.Entities.Common.V1.PersonName", "PersonName")
.WithMany()
.HasForeignKey("PersonNameId");

b.HasOne("JobLeet.WebApi.JobLeet.Core.Entities.Common.V1.Phone", "Phone")
.WithMany()
.HasForeignKey("PhoneId");
Expand All @@ -784,6 +793,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)

b.Navigation("Experience");

b.Navigation("PersonName");

b.Navigation("Phone");

b.Navigation("Qualifications");
Expand Down
24 changes: 24 additions & 0 deletions Server/JobLeet.WebApi/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash


execute_command() {
command="$1"
echo "Running: $command"
$command

if [ $? -ne 0 ]; then
echo "Command failed: $command"
exit 1
fi
}


execute_command "dotnet clean"


execute_command "dotnet restore"


execute_command "dotnet build"

execute_command "dotnet run"

0 comments on commit 7d55fcc

Please sign in to comment.