Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #228 from rinkudesu/unique-index-on-link-url
Browse files Browse the repository at this point in the history
Add unique index on link url for user
  • Loading branch information
KowalskiPiotr98 authored Apr 15, 2023
2 parents 44b9653 + e35ae4b commit c832ce7
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using Microsoft.EntityFrameworkCore;
using Rinkudesu.Services.Links.Utilities;

namespace Rinkudesu.Services.Links.Models
{
[ExcludeFromCodeCoverage]
[Index(nameof(LinkUrl), nameof(CreatingUserId), IsUnique = true)]
public class Link
{
[Key]
Expand Down Expand Up @@ -95,4 +97,4 @@ public enum LinkPrivacyOptions
Public
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
<ProjectReference Include="..\Rinkudesu.Services.Links.Utilities\Rinkudesu.Services.Links.Utilities.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ private async Task PopulateLinksAsync()
{
links = new List<Link>
{
new Link { CreatingUserId = _userId },
new Link { CreatingUserId = _userId, LinkUrl = Guid.NewGuid().ToString() },
new Link { LinkUrl = "http://localhost/", CreatingUserId = _userId },
new Link { Title = "ayaya", ShareableKey = "test", CreatingUserId = _userId },
new Link { Description = "tuturu*", CreatingUserId = Guid.NewGuid() },
new Link { PrivacyOptions = Link.LinkPrivacyOptions.Public, CreatingUserId = Guid.NewGuid() },
new Link { CreatingUserId = _userId }
new Link { Title = "ayaya", ShareableKey = "test", CreatingUserId = _userId, LinkUrl = Guid.NewGuid().ToString() },
new Link { Description = "tuturu*", CreatingUserId = Guid.NewGuid(), LinkUrl = Guid.NewGuid().ToString() },
new Link { PrivacyOptions = Link.LinkPrivacyOptions.Public, CreatingUserId = Guid.NewGuid(), LinkUrl = Guid.NewGuid().ToString() },
new Link { CreatingUserId = _userId, LinkUrl = Guid.NewGuid().ToString() }
};
_context.Links.AddRange(links);
await _context.SaveChangesAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public async Task GetKey_LinkSharedUserAuthorised_ReturnsLink()

private List<Link> GetLinks() => new()
{
new() { CreatingUserId = _userInfo.UserId },
new() { CreatingUserId = _userInfo.UserId, ShareableKey = "test" },
new() { CreatingUserId = Guid.NewGuid() },
new() { CreatingUserId = Guid.NewGuid(), ShareableKey = "test" },
new() { CreatingUserId = _userInfo.UserId, LinkUrl = Guid.NewGuid().ToString() },
new() { CreatingUserId = _userInfo.UserId, ShareableKey = "test", LinkUrl = Guid.NewGuid().ToString() },
new() { CreatingUserId = Guid.NewGuid(), LinkUrl = Guid.NewGuid().ToString() },
new() { CreatingUserId = Guid.NewGuid(), ShareableKey = "test", LinkUrl = Guid.NewGuid().ToString() },
};
}
}

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
@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Rinkudesu.Services.Links.Migrations
{
/// <inheritdoc />
public partial class AddUniqueUrlIndexForUser : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateIndex(
name: "IX_Links_LinkUrl_CreatingUserId",
table: "Links",
columns: new[] { "LinkUrl", "CreatingUserId" },
unique: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_Links_LinkUrl_CreatingUserId",
table: "Links");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,68 +1,71 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Rinkudesu.Services.Links.Data;

#nullable disable

namespace Rinkudesu.Services.Links.Migrations
{
[DbContext(typeof(LinkDbContext))]
partial class LinkDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.1")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("Rinkudesu.Services.Links.Models.Link", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<Guid>("CreatingUserId")
.HasColumnType("uuid");
b.Property<DateTime>("CreationDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("Description")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)");
b.Property<DateTime>("LastUpdate")
.HasColumnType("timestamp with time zone");
b.Property<string>("LinkUrl")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("character varying(200)");
b.Property<int>("PrivacyOptions")
.HasColumnType("integer");
b.Property<string>("ShareableKey")
.HasMaxLength(51)
.HasColumnType("character varying(51)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(250)
.HasColumnType("character varying(250)");
b.HasKey("Id");
b.ToTable("Links");
});
#pragma warning restore 612, 618
}
}
}
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Rinkudesu.Services.Links.Data;

#nullable disable

namespace Rinkudesu.Services.Links.Migrations
{
[DbContext(typeof(LinkDbContext))]
partial class LinkDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.1")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("Rinkudesu.Services.Links.Models.Link", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<Guid>("CreatingUserId")
.HasColumnType("uuid");
b.Property<DateTime>("CreationDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("Description")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)");
b.Property<DateTime>("LastUpdate")
.HasColumnType("timestamp with time zone");
b.Property<string>("LinkUrl")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("character varying(200)");
b.Property<int>("PrivacyOptions")
.HasColumnType("integer");
b.Property<string>("ShareableKey")
.HasMaxLength(51)
.HasColumnType("character varying(51)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(250)
.HasColumnType("character varying(250)");
b.HasKey("Id");
b.HasIndex("LinkUrl", "CreatingUserId")
.IsUnique();
b.ToTable("Links");
});
#pragma warning restore 612, 618
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RINKU_KAFKA_ADDRESS="empty" RINKU_KAFKA_USER="empty" RINKU_KAFKA_PASSWORD="empty" RINKU_KAFKA_CLIENT_ID="empty" RINKU_KAFKA_CONSUMER_GROUP_ID="empty" RINKUDESU_TAGS="empty" RINKU_LINKS_CONNECTIONSTRING="empty" dotnet ef migrations add $1

0 comments on commit c832ce7

Please sign in to comment.