Skip to content

Commit

Permalink
Add like (favorite) entity to database
Browse files Browse the repository at this point in the history
  • Loading branch information
marvac committed Feb 21, 2019
1 parent 807cacf commit 51c3a4b
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,26 @@ public class DataContext : DbContext
public DbSet<Value> Values { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Photo> Photos { get; set; }
public DbSet<Like> Likes { get; set; }

public DataContext(DbContextOptions<DataContext> options) : base(options)
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Like>().HasKey(k => new { k.LikerId, k.LikeeId });

modelBuilder.Entity<Like>()
.HasOne(u => u.Likee).WithMany(u => u.Likers)
.HasForeignKey(u => u.LikeeId)
.OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<Like>()
.HasOne(u => u.Liker).WithMany(u => u.Likees)
.HasForeignKey(u => u.LikerId)
.OnDelete(DeleteBehavior.Restrict);
}
}
}
135 changes: 135 additions & 0 deletions Migrations/20190221063605_AddLikeEntity.Designer.cs

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

45 changes: 45 additions & 0 deletions Migrations/20190221063605_AddLikeEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace Friendster.Migrations
{
public partial class AddLikeEntity : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Likes",
columns: table => new
{
LikeeId = table.Column<int>(nullable: false),
LikerId = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Likes", x => new { x.LikerId, x.LikeeId });
table.ForeignKey(
name: "FK_Likes_Users_LikeeId",
column: x => x.LikeeId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Likes_Users_LikerId",
column: x => x.LikerId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});

migrationBuilder.CreateIndex(
name: "IX_Likes_LikeeId",
table: "Likes",
column: "LikeeId");
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Likes");
}
}
}
28 changes: 27 additions & 1 deletion Migrations/DataContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,23 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.1.4-rtm-31024")
.HasAnnotation("ProductVersion", "2.1.8-servicing-32085")
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("Friendster.Models.Like", b =>
{
b.Property<int>("LikerId");

b.Property<int>("LikeeId");

b.HasKey("LikerId", "LikeeId");

b.HasIndex("LikeeId");

b.ToTable("Likes");
});

modelBuilder.Entity("Friendster.Models.Photo", b =>
{
b.Property<int>("Id")
Expand Down Expand Up @@ -94,6 +107,19 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("Values");
});

modelBuilder.Entity("Friendster.Models.Like", b =>
{
b.HasOne("Friendster.Models.User", "Likee")
.WithMany("Likers")
.HasForeignKey("LikeeId")
.OnDelete(DeleteBehavior.Restrict);

b.HasOne("Friendster.Models.User", "Liker")
.WithMany("Likees")
.HasForeignKey("LikerId")
.OnDelete(DeleteBehavior.Restrict);
});

modelBuilder.Entity("Friendster.Models.Photo", b =>
{
b.HasOne("Friendster.Models.User", "User")
Expand Down
15 changes: 15 additions & 0 deletions Models/Like.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Friendster.Models
{
public class Like
{
public int LikeeId { get; set; }
public int LikerId { get; set; }
public User Liker { get; set; }
public User Likee { get; set; }
}
}
8 changes: 8 additions & 0 deletions Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@ public class User
public string City { get; set; }
public string Country { get; set; }
public ICollection<Photo> Photos { get; set; }
/// <summary>
/// Users this user has liked
/// </summary>
public ICollection<Like> Likers { get; set; }
/// <summary>
/// Users that have liked this user
/// </summary>
public ICollection<Like> Likees { get; set; }
}
}

0 comments on commit 51c3a4b

Please sign in to comment.