Skip to content

Commit

Permalink
CM-50 Guess country (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
adedw authored Sep 21, 2024
1 parent 45dc9dc commit cfd3245
Show file tree
Hide file tree
Showing 26 changed files with 1,213 additions and 26 deletions.
4 changes: 3 additions & 1 deletion CommentMap.Mvc/Data/CommentMapDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ namespace CommentMap.Mvc.Data;
public class CommentMapDbContext(DbContextOptions<CommentMapDbContext> options) : IdentityDbContext<User, Role, Guid>(options), ICommentMapDbContext
{
public DbSet<Comment> Comments => Set<Comment>();
public DbSet<Country> Countries => Set<Country>();

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfiguration(new CommentConfiguration());
builder.ApplyConfiguration(new CommentConfiguration())
.ApplyConfiguration(new CountryConfiguration());
}
}
3 changes: 3 additions & 0 deletions CommentMap.Mvc/Data/Entities/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class Comment : IEquatable<Comment>

public bool IsDeleted { get; set; }

public Country? Country { get; set; }
public string? ISO3CodeCountry { get; set; }

public bool Equals(Comment? other)
{
if (other is null) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Configure(EntityTypeBuilder<Comment> builder)
.HasForeignKey(c => c.UserId)
.OnDelete(DeleteBehavior.Cascade);

builder.Property(c => c.Location).HasColumnType("geometry (point)");
builder.Property(c => c.Location).HasColumnType("geometry (point, 3857)");
builder.HasIndex(c => c.Location).HasMethod("gist");

builder.Property(c => c.Title).HasMaxLength(100);
Expand All @@ -23,5 +23,10 @@ public void Configure(EntityTypeBuilder<Comment> builder)
builder.Property(c => c.IsDeleted).HasDefaultValue(false);

builder.HasIndex(c => c.Title);

builder.HasOne(c => c.Country)
.WithMany(c => c.Comments)
.HasForeignKey(c => c.ISO3CodeCountry)
.OnDelete(DeleteBehavior.SetNull);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace CommentMap.Mvc.Data.Entities.Configurations;

public class CountryConfiguration : IEntityTypeConfiguration<Country>
{
public void Configure(EntityTypeBuilder<Country> builder)
{
builder.Property(c => c.Boundaries).HasColumnType("geometry (multipolygon, 3857)");
builder.HasIndex(c => c.Boundaries).HasMethod("gist");

builder.HasKey(c => c.ISO3Code);
builder.Property(c => c.ISO3Code).HasMaxLength(3).HasColumnType("char");
builder.Property(c => c.ISO2Code).HasMaxLength(2).HasColumnType("char");

builder.Property(c => c.Name).HasMaxLength(60);
builder.Property(c => c.RegionName).HasMaxLength(10);
builder.Property(c => c.SubregionName).HasMaxLength(40);
}
}
17 changes: 17 additions & 0 deletions CommentMap.Mvc/Data/Entities/Country.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using NetTopologySuite.Geometries;

namespace CommentMap.Mvc.Data.Entities;

public class Country
{
public required MultiPolygon Boundaries { get; set; }
public required string ISO3Code { get; set; }
public required string ISO2Code { get; set; }
public required string Name { get; set; }
public short RegionCode { get; set; }
public required string RegionName { get; set; }
public short SubregionCode { get; set; }
public required string SubregionName { get; set; }

public HashSet<Comment>? Comments { get; set; }
}
1 change: 1 addition & 0 deletions CommentMap.Mvc/Data/ICommentMapDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface ICommentMapDbContext
DbSet<User> Users { get; }
DbSet<Role> Roles { get; }
DbSet<Comment> Comments { get; }
DbSet<Country> Countries { get; }

Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}
Loading

0 comments on commit cfd3245

Please sign in to comment.