Skip to content

Commit

Permalink
Create initial database schema
Browse files Browse the repository at this point in the history
  • Loading branch information
zobweyt committed Jun 24, 2024
1 parent 5bbe333 commit 84e695a
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Giveaways.Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ namespace Giveaways.Data;
/// <param name="options">The options for this context.</param>
public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
// TODO: Configure the database context.
// Learn more at the https://learn.microsoft.com/ef/core.
public DbSet<Giveaway> Giveaways { get; set; }
public DbSet<GiveawayParticipant> GiveawayParticipants { get; set; }
}

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

66 changes: 66 additions & 0 deletions src/Giveaways.Data/Migrations/20240624181527_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Giveaways.Data.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Giveaways",
columns: table => new
{
MessageId = table.Column<ulong>(type: "INTEGER", nullable: false),
ChannelId = table.Column<ulong>(type: "INTEGER", nullable: false),
GuildId = table.Column<ulong>(type: "INTEGER", nullable: false),
Prize = table.Column<string>(type: "TEXT", nullable: false),
MaxWinners = table.Column<int>(type: "INTEGER", nullable: false),
ExpiresAt = table.Column<DateTime>(type: "TEXT", nullable: false),
Status = table.Column<int>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Giveaways", x => x.MessageId);
});

migrationBuilder.CreateTable(
name: "GiveawayParticipants",
columns: table => new
{
UserId = table.Column<ulong>(type: "INTEGER", nullable: false),
GiveawayId = table.Column<ulong>(type: "INTEGER", nullable: false),
IsWinner = table.Column<bool>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_GiveawayParticipants", x => new { x.UserId, x.GiveawayId });
table.ForeignKey(
name: "FK_GiveawayParticipants_Giveaways_GiveawayId",
column: x => x.GiveawayId,
principalTable: "Giveaways",
principalColumn: "MessageId",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "IX_GiveawayParticipants_GiveawayId",
table: "GiveawayParticipants",
column: "GiveawayId");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "GiveawayParticipants");

migrationBuilder.DropTable(
name: "Giveaways");
}
}
}
85 changes: 85 additions & 0 deletions src/Giveaways.Data/Migrations/AppDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// <auto-generated />
using System;
using Giveaways.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace Giveaways.Data.Migrations
{
[DbContext(typeof(AppDbContext))]
partial class AppDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.0");

modelBuilder.Entity("Giveaways.Data.Giveaway", b =>
{
b.Property<ulong>("MessageId")
.HasColumnType("INTEGER");

b.Property<ulong>("ChannelId")
.HasColumnType("INTEGER");

b.Property<DateTime>("ExpiresAt")
.HasColumnType("TEXT");

b.Property<ulong>("GuildId")
.HasColumnType("INTEGER");

b.Property<int>("MaxWinners")
.HasColumnType("INTEGER");

b.Property<string>("Prize")
.IsRequired()
.HasColumnType("TEXT");

b.Property<int>("Status")
.HasColumnType("INTEGER");

b.HasKey("MessageId");

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

modelBuilder.Entity("Giveaways.Data.GiveawayParticipant", b =>
{
b.Property<ulong>("UserId")
.HasColumnType("INTEGER");

b.Property<ulong>("GiveawayId")
.HasColumnType("INTEGER");

b.Property<bool>("IsWinner")
.HasColumnType("INTEGER");

b.HasKey("UserId", "GiveawayId");

b.HasIndex("GiveawayId");

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

modelBuilder.Entity("Giveaways.Data.GiveawayParticipant", b =>
{
b.HasOne("Giveaways.Data.Giveaway", "Giveaway")
.WithMany("Participants")
.HasForeignKey("GiveawayId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.Navigation("Giveaway");
});

modelBuilder.Entity("Giveaways.Data.Giveaway", b =>
{
b.Navigation("Participants");
});
#pragma warning restore 612, 618
}
}
}
54 changes: 54 additions & 0 deletions src/Giveaways.Data/Models/Giveaway.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Giveaways.Data;

/// <summary>
/// Represents a giveaway hosted by a user.
/// </summary>
public class Giveaway
{
/// <summary>
/// Gets the message identifier of the giveaway message.
/// </summary>
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public required ulong MessageId { get; init; }

/// <summary>
/// Gets the channel identifier where this giveaway was hosted.
/// </summary>
public required ulong ChannelId { get; init; }

/// <summary>
/// Gets the guild identifier where this giveaway was hosted.
/// </summary>
public required ulong GuildId { get; init; }

/// <summary>
/// Gets or sets the prize of this giveaway.
/// </summary>
public required string Prize { get; set; }

/// <summary>
/// Gets or sets the maximum number of winners for this giveaway.
/// </summary>
public required int MaxWinners { get; set; }

/// <summary>
/// Gets or sets the expiration date and time for this giveaway.
/// </summary>
public required DateTime ExpiresAt { get; set; }

/// <summary>
/// Gets or sets the current status of this giveaway.
/// </summary>
public GiveawayStatus Status { get; set; } = GiveawayStatus.Active;

/// <summary>
/// Gets the list of participants for this giveaway.
/// </summary>
public List<GiveawayParticipant> Participants { get; } = [];
}
32 changes: 32 additions & 0 deletions src/Giveaways.Data/Models/GiveawayParticipant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace Giveaways.Data;

/// <summary>
/// Represents a participant of a giveaway.
/// </summary>
[PrimaryKey(nameof(UserId), nameof(GiveawayId))]
public class GiveawayParticipant
{
/// <summary>
/// Gets or sets the user ID of the participant.
/// </summary>
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public ulong UserId { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the participant is a winner.
/// </summary>
public bool IsWinner { get; set; } = false;

/// <summary>
/// Gets or sets the ID of the giveaway the participant is associated with.
/// </summary>
public ulong GiveawayId { get; set; }

/// <summary>
/// Gets or sets the reference to the giveaway the participant is associated with.
/// </summary>
public Giveaway Giveaway { get; set; } = null!;
}
22 changes: 22 additions & 0 deletions src/Giveaways.Data/Models/GiveawayStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Giveaways;

/// <summary>
/// Represents the status of a giveaway.
/// </summary>
public enum GiveawayStatus
{
/// <summary>
/// The giveaway is currently active and participants can still enter.
/// </summary>
Active,

/// <summary>
/// The giveaway is temporarily suspended and not accepting new entries.
/// </summary>
Suspended,

/// <summary>
/// The giveaway has ended, and winners have been selected.
/// </summary>
Ended,
}

0 comments on commit 84e695a

Please sign in to comment.