Skip to content

Commit 1f8aa21

Browse files
Add project updates and random project retrieval
- Added index on `ProjectName` in the database for uniqueness. - Implemented `UpdateProjectAsync` in `ProjectService` to handle project updates. - Introduced `GetRandomPublicAsync` method to retrieve a random selection of public projects. - Updated DTOs for improved response handling. - Extended `ProjectsController` with endpoints for random project retrieval and updates.
1 parent 940e0af commit 1f8aa21

File tree

10 files changed

+335
-31
lines changed

10 files changed

+335
-31
lines changed

Migrations/20251119191413_Members_project.Designer.cs

Lines changed: 189 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace sparkly_server.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class Members_project : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.CreateIndex(
14+
name: "IX_projects_ProjectName",
15+
table: "projects",
16+
column: "ProjectName",
17+
unique: true);
18+
}
19+
20+
/// <inheritdoc />
21+
protected override void Down(MigrationBuilder migrationBuilder)
22+
{
23+
migrationBuilder.DropIndex(
24+
name: "IX_projects_ProjectName",
25+
table: "projects");
26+
}
27+
}
28+
}

Migrations/AppDbContextModelSnapshot.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
110110

111111
b.HasKey("Id");
112112

113+
b.HasIndex("ProjectName")
114+
.IsUnique();
115+
113116
b.ToTable("projects", (string)null);
114117
});
115118

Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using sparkly_server.Services.UserServices;
99
using System.Text;
1010
using Scalar.AspNetCore;
11+
using sparkly_server.Services.Projects;
1112

1213
namespace sparkly_server;
1314

@@ -16,7 +17,6 @@ public class Program
1617
public static void Main(string[] args)
1718
{
1819
var builder = WebApplication.CreateBuilder(args);
19-
2020

2121
var jwtKey = builder.Configuration["SPARKLY_JWT_KEY"]
2222
?? Environment.GetEnvironmentVariable("SPARKLY_JWT_KEY");
@@ -50,11 +50,14 @@ public static void Main(string[] args)
5050
policy.RequireRole(Roles.Admin));
5151
});
5252

53+
// Services
5354
builder.Services.AddScoped<IUserRepository, UserRepository>();
5455
builder.Services.AddScoped<IUserService, UserService>();
5556
builder.Services.AddScoped<IJwtProvider, JwtProvider>();
5657
builder.Services.AddScoped<IAuthService, AuthService>();
5758
builder.Services.AddScoped<ICurrentUser, CurrentUser>();
59+
builder.Services.AddScoped<IProjectRepository, ProjectRepository>();
60+
builder.Services.AddScoped<IProjectService, ProjectService>();
5861

5962
var connectionString = builder.Configuration.GetConnectionString("Default")
6063
?? Environment.GetEnvironmentVariable("ConnectionStrings__Default")
@@ -66,7 +69,7 @@ public static void Main(string[] args)
6669
});
6770

6871
builder.Services.AddControllers();
69-
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
72+
7073
builder.Services.AddOpenApi();
7174

7275
builder.Services.AddCors(options =>

src/Controllers/Projects/ProjectsController.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,38 @@ public ProjectsController(IProjectService projects, ICurrentUser currentUser, IU
2424
_users = users;
2525
}
2626

27-
// [HttpGet("")]
28-
// public async GetProjectGlobal([FromQuery] int take = 10, CancellationToken ct = default)
29-
// {
30-
//
31-
// }
32-
27+
[HttpGet("random")]
28+
public async Task<IActionResult> GetRandomProjects([FromQuery] int take = 20, CancellationToken ct = default)
29+
{
30+
var response = await _projects.GetRandomPublicAsync(take, ct);
31+
return Ok(response);
32+
}
3333

3434
[HttpPost("create")]
3535
public async Task<IActionResult> CreateProject([FromBody] CreateProjectRequest request)
3636
{
3737
var project = await _projects.CreateProjectAsync(request.ProjectName, request.Description, request.Visibility);
3838

39-
var response = new ProjectResponse(
40-
Id: project.Id,
41-
ProjectName: project.ProjectName,
42-
Description: project.Description,
43-
Visibility: project.Visibility,
44-
OwnerId: project.OwnerId
45-
);
39+
var response = new ProjectResponse(project);
4640

4741
return Ok(response);
4842
}
4943

50-
// [HttpPut("update/{projectId:guid}")]
51-
// public async Task<IActionResult> UpdateProject(Guid id, [FromBody] UpdateProjectRequest request, CancellationToken cn)
52-
// {
53-
// var project = await _projects.GetProjectByIdAsync(projectId: id, cn);
54-
//
55-
// project.up
56-
// }
44+
[HttpGet("{projectId:guid}")]
45+
public async Task<IActionResult> GetProjectById(Guid projectId, CancellationToken ct = default)
46+
{
47+
var project = await _projects.GetProjectByIdAsync(projectId, ct);
48+
return Ok(project);
49+
}
50+
51+
[HttpPut("update/{projectId:guid}")]
52+
public async Task<IActionResult> UpdateProject(
53+
Guid projectId,
54+
[FromBody] UpdateProjectRequest request,
55+
CancellationToken cn = default)
56+
{
57+
await _projects.UpdateProjectAsync(projectId, request, cn);
58+
return NoContent();
59+
}
5760
}
5861
}
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
using sparkly_server.Domain.Projects;
12
using sparkly_server.Enum;
23

34
namespace sparkly_server.DTO.Projects
45
{
5-
public sealed record ProjectResponse(
6-
Guid Id,
7-
string ProjectName,
8-
string Description,
9-
ProjectVisibility Visibility,
10-
Guid OwnerId
11-
);
6+
public sealed record ProjectResponse
7+
{
8+
public Guid Id { get; }
9+
public string ProjectName { get; }
10+
public string Description { get; }
11+
public ProjectVisibility Visibility { get; }
12+
public Guid OwnerId { get; }
13+
14+
public ProjectResponse(Project project)
15+
{
16+
Id = project.Id;
17+
ProjectName = project.ProjectName;
18+
Description = project.Description;
19+
Visibility = project.Visibility;
20+
OwnerId = project.OwnerId;
21+
}
22+
}
23+
1224
}

src/Services/Projects/IProjectRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public interface IProjectRepository
88
Task<Project?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
99
Task<IReadOnlyList<Project>> GetForUserAsync(Guid userId, CancellationToken cancellationToken = default);
1010
Task<bool> IsProjectNameTakenAsync(string projectName, CancellationToken cn);
11-
11+
Task<IReadOnlyList<Project>> GetRandomPublicAsync(int take, CancellationToken ct = default);
1212
Task SaveChangesAsync(CancellationToken cancellationToken = default);
1313
}
1414
}

src/Services/Projects/IProjectService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using sparkly_server.Domain.Projects;
2+
using sparkly_server.DTO.Projects;
23
using sparkly_server.Enum;
34

45
namespace sparkly_server.Services.Projects
@@ -42,5 +43,14 @@ Task RemoveMemberAsync(
4243
Guid projectId,
4344
Guid userId,
4445
CancellationToken cancellationToken = default);
46+
47+
Task<IReadOnlyList<ProjectResponse>> GetRandomPublicAsync(
48+
int take,
49+
CancellationToken ct = default);
50+
51+
Task UpdateProjectAsync(
52+
Guid projectId,
53+
UpdateProjectRequest request,
54+
CancellationToken cancellationToken = default);
4555
}
4656
}

0 commit comments

Comments
 (0)