Skip to content

Commit 7f83107

Browse files
Merge pull request #8 from chenxidev1129/7-add-createdat-and-updatedat-into-the-tasks-table
fix tasks table
2 parents 2123224 + 33240ad commit 7f83107

File tree

6 files changed

+68
-75
lines changed

6 files changed

+68
-75
lines changed

taskmaster-api/Data/DTOs/TaskDto.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,27 @@ namespace taskmaster_api.Data.DTOs
66
{
77
public class TaskDto : IDto<TaskEntity>
88
{
9-
public int Id { get; set; }
9+
public int? Id { get; set; }
1010
public string Title { get; set; }
11-
public string Description { get; set; }
12-
public DateTime DueDate { get; set; }
11+
public string? Description { get; set; }
12+
public DateTime? DueDate { get; set; }
1313
public string Status { get; set; }
14+
public DateTime CreatedAt { get; set; }
15+
public DateTime UpdatedAt { get; set; }
16+
17+
public TaskDto()
18+
{
19+
Status = Models.TaskStatus.Pending;
20+
if (Id.HasValue)
21+
{
22+
UpdatedAt = DateTime.UtcNow;
23+
}
24+
else
25+
{
26+
CreatedAt = DateTime.UtcNow;
27+
UpdatedAt = DateTime.UtcNow;
28+
}
29+
}
1430

1531
public TaskEntity ToEntity()
1632
{

taskmaster-api/Data/Entities/TaskEntity.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
2+
using Microsoft.EntityFrameworkCore.Metadata.Internal;
3+
using Microsoft.EntityFrameworkCore;
4+
using System.ComponentModel;
5+
using System.ComponentModel.DataAnnotations;
6+
using System.ComponentModel.DataAnnotations.Schema;
27
using taskmaster_api.Data.DTOs;
38
using taskmaster_api.Data.Entities.Interface;
49
using taskmaster_api.Utilities;
10+
using System.Runtime.Serialization;
511

612
namespace taskmaster_api.Data.Entities
713
{
814
public class TaskEntity : IEntity<TaskDto>
915
{
1016
[Key]
11-
public int Id { get; set; }
17+
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
18+
public int? Id { get; set; }
1219

1320
[Required]
1421
public string Title { get; set; }
1522

16-
public string Description { get; set; }
23+
public string? Description { get; set; }
1724

18-
public DateTime DueDate { get; set; }
25+
public DateTime? DueDate { get; set; }
1926

27+
[StringLength(50)]
2028
public string Status { get; set; }
2129

30+
public DateTime CreatedAt { get; set; }
31+
32+
public DateTime UpdatedAt { get; set; }
33+
2234
public TaskDto ToDto()
2335
{
2436
return EntityHelpers.ToDto<TaskEntity, TaskDto>(this);

taskmaster-api/Data/Models/TaskStatus.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
public static class TaskStatus
44
{
5+
public const string Pending = "Pending";
56
public const string Open = "Open";
67
public const string InProgress = "InProgress";
78
public const string Completed = "Complete";

taskmaster-api/Migrations/20231208193605_InitialCreate.Designer.cs renamed to taskmaster-api/Migrations/20231211053644_InitialCreate.Designer.cs

Lines changed: 14 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

taskmaster-api/Migrations/20231208193605_InitialCreate.cs renamed to taskmaster-api/Migrations/20231211053644_InitialCreate.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,17 @@ protected override void Up(MigrationBuilder migrationBuilder)
5757
Id = table.Column<int>(type: "int", nullable: false)
5858
.Annotation("SqlServer:Identity", "1, 1"),
5959
Title = table.Column<string>(type: "nvarchar(max)", nullable: false),
60-
Description = table.Column<string>(type: "nvarchar(max)", nullable: false),
61-
DueDate = table.Column<DateTime>(type: "datetime2", nullable: false)
60+
Description = table.Column<string>(type: "nvarchar(max)", nullable: true),
61+
DueDate = table.Column<DateTime>(type: "datetime2", nullable: true),
62+
Status = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
63+
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
64+
UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: false)
6265
},
6366
constraints: table =>
6467
{
6568
table.PrimaryKey("PK_Tasks", x => x.Id);
6669
});
6770

68-
migrationBuilder.CreateTable(
69-
name: "Users",
70-
columns: table => new
71-
{
72-
Id = table.Column<int>(type: "int", nullable: false)
73-
.Annotation("SqlServer:Identity", "1, 1"),
74-
Username = table.Column<string>(type: "nvarchar(max)", nullable: false),
75-
Email = table.Column<string>(type: "nvarchar(max)", nullable: false)
76-
},
77-
constraints: table =>
78-
{
79-
table.PrimaryKey("PK_Users", x => x.Id);
80-
});
81-
8271
migrationBuilder.CreateTable(
8372
name: "AspNetRoleClaims",
8473
columns: table => new
@@ -246,9 +235,6 @@ protected override void Down(MigrationBuilder migrationBuilder)
246235
migrationBuilder.DropTable(
247236
name: "Tasks");
248237

249-
migrationBuilder.DropTable(
250-
name: "Users");
251-
252238
migrationBuilder.DropTable(
253239
name: "AspNetRoles");
254240

taskmaster-api/Migrations/ApplicationDbContextModelSnapshot.cs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -222,47 +222,36 @@ protected override void BuildModel(ModelBuilder modelBuilder)
222222

223223
modelBuilder.Entity("taskmaster_api.Data.Entities.TaskEntity", b =>
224224
{
225-
b.Property<int>("Id")
225+
b.Property<int?>("Id")
226226
.ValueGeneratedOnAdd()
227227
.HasColumnType("int");
228228

229-
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
229+
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int?>("Id"));
230+
231+
b.Property<DateTime>("CreatedAt")
232+
.HasColumnType("datetime2");
230233

231234
b.Property<string>("Description")
232-
.IsRequired()
233235
.HasColumnType("nvarchar(max)");
234236

235-
b.Property<DateTime>("DueDate")
237+
b.Property<DateTime?>("DueDate")
236238
.HasColumnType("datetime2");
237239

238-
b.Property<string>("Title")
240+
b.Property<string>("Status")
239241
.IsRequired()
240-
.HasColumnType("nvarchar(max)");
242+
.HasMaxLength(50)
243+
.HasColumnType("nvarchar(50)");
241244

242-
b.HasKey("Id");
243-
244-
b.ToTable("Tasks");
245-
});
246-
247-
modelBuilder.Entity("taskmaster_api.Data.Entities.UserEntity", b =>
248-
{
249-
b.Property<int>("Id")
250-
.ValueGeneratedOnAdd()
251-
.HasColumnType("int");
252-
253-
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
254-
255-
b.Property<string>("Email")
245+
b.Property<string>("Title")
256246
.IsRequired()
257247
.HasColumnType("nvarchar(max)");
258248

259-
b.Property<string>("Username")
260-
.IsRequired()
261-
.HasColumnType("nvarchar(max)");
249+
b.Property<DateTime>("UpdatedAt")
250+
.HasColumnType("datetime2");
262251

263252
b.HasKey("Id");
264253

265-
b.ToTable("Users");
254+
b.ToTable("Tasks");
266255
});
267256

268257
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>

0 commit comments

Comments
 (0)