Skip to content

Commit 4cfa276

Browse files
add table and endpoints for activity log
1 parent e21a9bf commit 4cfa276

File tree

12 files changed

+424
-5
lines changed

12 files changed

+424
-5
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using taskmaster_api.Data.DTOs;
4+
using taskmaster_api.Services.Interface;
5+
6+
namespace taskmaster_api.Controllers
7+
{
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class ActivityLogController : ApplicationControllerBase
11+
{
12+
private readonly IActivityLogService _activityLogService;
13+
14+
public ActivityLogController(IActivityLogService activityLogService)
15+
{
16+
_activityLogService = activityLogService;
17+
}
18+
19+
[HttpGet]
20+
public IActionResult GetAllActivityLogs()
21+
{
22+
return ToHttpResult<List<ActivityLogDto>>(_activityLogService.GetAllActivityLogs());
23+
}
24+
25+
[HttpGet("{id}")]
26+
public IActionResult GetActivityLog(int id)
27+
{
28+
return ToHttpResult<ActivityLogDto>(_activityLogService.GetActivityLogById(id));
29+
}
30+
31+
[HttpPost]
32+
public IActionResult CreateActivityLog(ActivityLogDto activityLogDto)
33+
{
34+
return ToHttpResult<ActivityLogDto>(_activityLogService.CreateActivityLog(activityLogDto));
35+
}
36+
37+
[HttpPut("{id}")]
38+
public IActionResult UpdateActivityLog(int id, ActivityLogDto activityLogDto)
39+
{
40+
return ToHttpResult<ActivityLogDto>(_activityLogService.UpdateActivityLog(id, activityLogDto));
41+
}
42+
43+
[HttpDelete("{id}")]
44+
public IActionResult DeleteActivityLog(int id)
45+
{
46+
return ToHttpResult(_activityLogService.DeleteActivityLog(id));
47+
}
48+
}
49+
}

taskmaster-api/Data/Contexts/ApplicationDbContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public ApplicationDbContext(DbContextOptions options) : base(options)
1515
public virtual DbSet<CommentEntity> Comments { get; set; }
1616
public virtual DbSet<AttachmentEntity> Attachments { get; set; }
1717
public virtual DbSet<TagEntity> Tags { get; set; }
18+
public virtual DbSet<ActivityLogEntity> ActivityLogs { get; set; }
1819
}
1920
}
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
namespace taskmaster_api.Data.DTOs
1+
using taskmaster_api.Data.DTOs.Interface;
2+
using taskmaster_api.Data.Entities;
3+
using taskmaster_api.Utilities;
4+
5+
namespace taskmaster_api.Data.DTOs
26
{
3-
public class ActivityLogDto
7+
public class ActivityLogDto : IDto<ActivityLogEntity>
48
{
9+
public int? Id { get; set; }
10+
public string UserId { get; set; }
11+
public string Action { get; set; }
12+
public DateTime CreatedAt { get; set; }
13+
14+
public ActivityLogDto()
15+
{
16+
if (!Id.HasValue)
17+
{
18+
CreatedAt = DateTime.UtcNow;
19+
}
20+
}
21+
22+
public ActivityLogEntity ToEntity()
23+
{
24+
return EntityHelpers.ToEntity<ActivityLogDto, ActivityLogEntity>(this);
25+
}
526
}
627
}
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
1-
namespace taskmaster_api.Data.Entities
1+
using Microsoft.AspNetCore.Identity;
2+
using System.ComponentModel.DataAnnotations.Schema;
3+
using System.ComponentModel.DataAnnotations;
4+
using taskmaster_api.Data.DTOs;
5+
using taskmaster_api.Utilities;
6+
using taskmaster_api.Data.Entities.Interface;
7+
8+
namespace taskmaster_api.Data.Entities
29
{
3-
public class ActivityLogEntity
10+
public class ActivityLogEntity : IEntity<ActivityLogDto>
411
{
12+
[Key]
13+
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
14+
public int? Id { get; set; }
15+
16+
[Required]
17+
public string UserId { get; set; }
18+
public IdentityUser User { get; set; }
19+
20+
[Required]
21+
[StringLength(100)]
22+
public string Action { get; set; }
23+
24+
public DateTime CreatedAt { get; set; }
25+
26+
public ActivityLogDto ToDto()
27+
{
28+
return EntityHelpers.ToDto<ActivityLogEntity, ActivityLogDto>(this);
29+
}
530
}
631
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using taskmaster_api.Data.Contexts;
3+
using taskmaster_api.Data.Entities;
4+
using taskmaster_api.Data.Repositories.Interface;
5+
6+
namespace taskmaster_api.Data.Repositories
7+
{
8+
public class ActivityLogRepository : IActivityLogRepository
9+
{
10+
private readonly ApplicationDbContext _context;
11+
12+
public ActivityLogRepository(ApplicationDbContext context)
13+
{
14+
_context = context;
15+
}
16+
17+
public ActivityLogEntity GetActivityLogById(int id)
18+
{
19+
return _context.ActivityLogs.Find(id);
20+
}
21+
22+
public IEnumerable<ActivityLogEntity> GetAllActivityLogs()
23+
{
24+
return _context.ActivityLogs.ToList();
25+
}
26+
27+
public ActivityLogEntity CreateActivityLog(ActivityLogEntity activityLog)
28+
{
29+
_context.ActivityLogs.Add(activityLog);
30+
_context.SaveChanges();
31+
return activityLog;
32+
}
33+
34+
public ActivityLogEntity UpdateActivityLog(int id, ActivityLogEntity activityLog)
35+
{
36+
if (_context.ActivityLogs.Find(id) is ActivityLogEntity oldActivityLog)
37+
{
38+
activityLog.Id = id;
39+
_context.ActivityLogs.Entry(oldActivityLog).State = EntityState.Detached;
40+
_context.ActivityLogs.Entry(activityLog).State = EntityState.Modified;
41+
_context.SaveChanges();
42+
}
43+
return activityLog;
44+
}
45+
46+
public int DeleteActivityLog(int id)
47+
{
48+
var activityLogToDelete = _context.ActivityLogs.Find(id);
49+
if (activityLogToDelete != null)
50+
{
51+
_context.ActivityLogs.Remove(activityLogToDelete);
52+
_context.SaveChanges();
53+
return id;
54+
}
55+
return -1;
56+
}
57+
}
58+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using taskmaster_api.Data.Entities;
2+
3+
namespace taskmaster_api.Data.Repositories.Interface
4+
{
5+
public interface IActivityLogRepository
6+
{
7+
ActivityLogEntity GetActivityLogById(int id);
8+
IEnumerable<ActivityLogEntity> GetAllActivityLogs();
9+
ActivityLogEntity CreateActivityLog(ActivityLogEntity activityLog);
10+
ActivityLogEntity UpdateActivityLog(int id, ActivityLogEntity activityLog);
11+
int DeleteActivityLog(int id);
12+
}
13+
}

taskmaster-api/Migrations/20231211082219_InitialCreate.Designer.cs renamed to taskmaster-api/Migrations/20231211083537_InitialCreate.Designer.cs

Lines changed: 39 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

taskmaster-api/Migrations/20231211082219_InitialCreate.cs renamed to taskmaster-api/Migrations/20231211083537_InitialCreate.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,27 @@ protected override void Up(MigrationBuilder migrationBuilder)
102102
onDelete: ReferentialAction.Cascade);
103103
});
104104

105+
migrationBuilder.CreateTable(
106+
name: "ActivityLogs",
107+
columns: table => new
108+
{
109+
Id = table.Column<int>(type: "int", nullable: false)
110+
.Annotation("SqlServer:Identity", "1, 1"),
111+
UserId = table.Column<string>(type: "nvarchar(450)", nullable: false),
112+
Action = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
113+
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false)
114+
},
115+
constraints: table =>
116+
{
117+
table.PrimaryKey("PK_ActivityLogs", x => x.Id);
118+
table.ForeignKey(
119+
name: "FK_ActivityLogs_AspNetUsers_UserId",
120+
column: x => x.UserId,
121+
principalTable: "AspNetUsers",
122+
principalColumn: "Id",
123+
onDelete: ReferentialAction.Cascade);
124+
});
125+
105126
migrationBuilder.CreateTable(
106127
name: "AspNetUserClaims",
107128
columns: table => new
@@ -245,6 +266,11 @@ protected override void Up(MigrationBuilder migrationBuilder)
245266
onDelete: ReferentialAction.Cascade);
246267
});
247268

269+
migrationBuilder.CreateIndex(
270+
name: "IX_ActivityLogs_UserId",
271+
table: "ActivityLogs",
272+
column: "UserId");
273+
248274
migrationBuilder.CreateIndex(
249275
name: "IX_AspNetRoleClaims_RoleId",
250276
table: "AspNetRoleClaims",
@@ -308,6 +334,9 @@ protected override void Up(MigrationBuilder migrationBuilder)
308334
/// <inheritdoc />
309335
protected override void Down(MigrationBuilder migrationBuilder)
310336
{
337+
migrationBuilder.DropTable(
338+
name: "ActivityLogs");
339+
311340
migrationBuilder.DropTable(
312341
name: "AspNetRoleClaims");
313342

taskmaster-api/Migrations/ApplicationDbContextModelSnapshot.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,33 @@ protected override void BuildModel(ModelBuilder modelBuilder)
220220
b.ToTable("AspNetUserTokens", (string)null);
221221
});
222222

223+
modelBuilder.Entity("taskmaster_api.Data.Entities.ActivityLogEntity", b =>
224+
{
225+
b.Property<int?>("Id")
226+
.ValueGeneratedOnAdd()
227+
.HasColumnType("int");
228+
229+
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int?>("Id"));
230+
231+
b.Property<string>("Action")
232+
.IsRequired()
233+
.HasMaxLength(100)
234+
.HasColumnType("nvarchar(100)");
235+
236+
b.Property<DateTime>("CreatedAt")
237+
.HasColumnType("datetime2");
238+
239+
b.Property<string>("UserId")
240+
.IsRequired()
241+
.HasColumnType("nvarchar(450)");
242+
243+
b.HasKey("Id");
244+
245+
b.HasIndex("UserId");
246+
247+
b.ToTable("ActivityLogs");
248+
});
249+
223250
modelBuilder.Entity("taskmaster_api.Data.Entities.AttachmentEntity", b =>
224251
{
225252
b.Property<int?>("Id")
@@ -392,6 +419,17 @@ protected override void BuildModel(ModelBuilder modelBuilder)
392419
.IsRequired();
393420
});
394421

422+
modelBuilder.Entity("taskmaster_api.Data.Entities.ActivityLogEntity", b =>
423+
{
424+
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", "User")
425+
.WithMany()
426+
.HasForeignKey("UserId")
427+
.OnDelete(DeleteBehavior.Cascade)
428+
.IsRequired();
429+
430+
b.Navigation("User");
431+
});
432+
395433
modelBuilder.Entity("taskmaster_api.Data.Entities.AttachmentEntity", b =>
396434
{
397435
b.HasOne("taskmaster_api.Data.Entities.TaskEntity", "Task")

taskmaster-api/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@
6363
builder.Services.AddScoped<ICommentRepository, CommentRepository>();
6464
builder.Services.AddScoped<IAttachmentRepository, AttachmentRepository>();
6565
builder.Services.AddScoped<ITagRepository, TagRepository>();
66+
builder.Services.AddScoped<IActivityLogRepository, ActivityLogRepository>();
6667

6768
builder.Services.AddScoped<ITaskService, TaskService>();
6869
builder.Services.AddScoped<IAuthService, AuthService>();
6970
builder.Services.AddScoped<IUserService, UserService>();
7071
builder.Services.AddScoped<ICommentService, CommentService>();
7172
builder.Services.AddScoped<IAttachmentService, AttachmentService>();
7273
builder.Services.AddScoped<ITagService, TagService>();
74+
builder.Services.AddScoped<IActivityLogService, ActivityLogService>();
7375

7476
var app = builder.Build();
7577

0 commit comments

Comments
 (0)