-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0f08ee
commit 1aedb7c
Showing
13 changed files
with
364 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Application.Common.DTOs.Folders; | ||
|
||
public class FolderInDto | ||
{ | ||
public string Guid { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public string Color { get; set; } | ||
|
||
public string Icon { get; set; } | ||
|
||
public string Description { get; set; } | ||
|
||
public string LastModified { get; set; } | ||
|
||
public ICollection<FolderInDto> Children { get; set; } = new List<FolderInDto>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.ObjectModel; | ||
|
||
namespace Application.Common.DTOs.Folders; | ||
|
||
public class FolderOutDto | ||
{ | ||
public string Guid { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public string Color { get; set; } | ||
|
||
public string Icon { get; set; } | ||
|
||
public string Description { get; set; } | ||
|
||
public string LastModified { get; set; } | ||
|
||
public ICollection<FolderOutDto> Children { get; set; } = new Collection<FolderOutDto>(); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Application/Interfaces/Repositories/IFolderRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Domain.Entities; | ||
|
||
namespace Application.Interfaces.Repositories; | ||
|
||
public interface IFolderRepository | ||
{ | ||
public Task SaveChangesAsync(); | ||
public Task<Folder> GetFolderAsync(Guid folderId); | ||
public Task CreateFolderAsync(Folder folder); | ||
public void RemoveFolder(Folder folder); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Application.Common.DTOs.Folders; | ||
|
||
namespace Application.Interfaces.Services; | ||
|
||
public interface IFolderService | ||
{ | ||
public Task UpdateFoldersAsync(string email, FolderInDto folderInDto); | ||
public Task<FolderOutDto> GetFoldersAsync(string email); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Application.Common.DTOs.Folders; | ||
using Application.Common.Exceptions; | ||
using Application.Interfaces.Repositories; | ||
using Application.Interfaces.Services; | ||
using Domain.Entities; | ||
|
||
namespace Application.Services; | ||
|
||
public class FolderService(IUserRepository userRepository, IFolderRepository folderRepository) : IFolderService | ||
{ | ||
public IUserRepository UserRepository { get; } = userRepository; | ||
public IFolderRepository FolderRepository { get; } = folderRepository; | ||
|
||
public async Task UpdateFoldersAsync(string email, FolderInDto folderInDto) | ||
{ | ||
var user = await UserRepository.GetAsync(email, true); | ||
if (user == null) | ||
throw new CommonErrorException(400, "No user with this email exists", 17); | ||
|
||
var folder = FolderInDtoToFolder(folderInDto); | ||
// If the user has no root folder, create it and set it as the user's root folder | ||
if(user.RootFolderId == Guid.Empty) | ||
{ | ||
await FolderRepository.CreateFolderAsync(folder); | ||
user.RootFolderId = folder.FolderId; | ||
|
||
await UserRepository.SaveChangesAsync(); | ||
return; | ||
} | ||
|
||
if(folder.FolderId != user.RootFolderId) | ||
throw new CommonErrorException(400, "Folder id does not match user root folder id", 0); | ||
|
||
var existingFolder = await FolderRepository.GetFolderAsync(user.RootFolderId); | ||
if(existingFolder == null) | ||
throw new CommonErrorException(400, "User has no root folder", 0); | ||
|
||
FolderRepository.RemoveFolder(existingFolder); | ||
await FolderRepository.CreateFolderAsync(folder); | ||
await FolderRepository.SaveChangesAsync(); | ||
} | ||
|
||
private Folder FolderInDtoToFolder(FolderInDto folderInDto) | ||
{ | ||
var folder = new Folder | ||
{ | ||
FolderId = new Guid(folderInDto.Guid), | ||
Name = folderInDto.Name, | ||
Color = folderInDto.Color, | ||
Icon = folderInDto.Icon, | ||
Description = folderInDto.Description, | ||
LastModified = folderInDto.LastModified, | ||
Children = new List<Folder>() | ||
}; | ||
|
||
foreach (var child in folderInDto.Children) | ||
{ | ||
var childFolder = FolderInDtoToFolder(child); | ||
folder.Children.Add(childFolder); | ||
} | ||
|
||
return folder; | ||
} | ||
|
||
public async Task<FolderOutDto> GetFoldersAsync(string email) | ||
{ | ||
var user = await UserRepository.GetAsync(email, false); | ||
if (user == null) | ||
{ | ||
throw new CommonErrorException(400, "No user with this email exists", 17); | ||
} | ||
|
||
if(user.RootFolderId == Guid.Empty) | ||
{ | ||
throw new CommonErrorException(400, "User has no root folder", 22); | ||
} | ||
|
||
var folder = await FolderRepository.GetFolderAsync(user.RootFolderId); | ||
return await FolderToFolderOutDto(folder); | ||
} | ||
|
||
private async Task<FolderOutDto> FolderToFolderOutDto(Folder folder) | ||
{ | ||
var folderOutDto = new FolderOutDto | ||
{ | ||
Guid = folder.FolderId.ToString(), | ||
Name = folder.Name, | ||
Color = folder.Color, | ||
Icon = folder.Icon, | ||
LastModified = folder.LastModified, | ||
Description = folder.Description | ||
}; | ||
|
||
foreach (var child in folder.Children) | ||
{ | ||
var childFolderOutDto = await FolderToFolderOutDto(child); | ||
folderOutDto.Children.Add(childFolderOutDto); | ||
} | ||
|
||
return folderOutDto; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#nullable enable | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace Domain.Entities; | ||
|
||
public class Folder | ||
{ | ||
[DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
[Key] | ||
public Guid FolderId { get; set; } | ||
|
||
[Required] | ||
public string Name { get; set; } | ||
Check warning on line 14 in src/Domain/Entities/Folder.cs GitHub Actions / deploy
|
||
|
||
[Required] | ||
public string Color { get; set; } | ||
Check warning on line 17 in src/Domain/Entities/Folder.cs GitHub Actions / deploy
|
||
|
||
[Required] | ||
public string Icon { get; set; } | ||
Check warning on line 20 in src/Domain/Entities/Folder.cs GitHub Actions / deploy
|
||
|
||
public string Description { get; set; } | ||
Check warning on line 22 in src/Domain/Entities/Folder.cs GitHub Actions / deploy
|
||
|
||
[Required] | ||
public string LastModified { get; set; } | ||
Check warning on line 25 in src/Domain/Entities/Folder.cs GitHub Actions / deploy
|
||
|
||
public Guid? ParentFolderId { get; set; } | ||
public Folder? ParentFolder { get; set; } | ||
public List<Folder>? Children { get; set; } = new List<Folder>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Infrastructure/Persistence/EntityConfigurations/FolderConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Domain.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace Infrastructure.Persistence.EntityConfigurations; | ||
|
||
public class FolderConfiguration : IEntityTypeConfiguration<Folder> | ||
{ | ||
public void Configure(EntityTypeBuilder<Folder> builder) | ||
{ | ||
builder | ||
.HasOne(f => f.ParentFolder) | ||
.WithMany(f => f.Children) | ||
.HasForeignKey(f => f.ParentFolderId) | ||
.OnDelete(DeleteBehavior.NoAction); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Infrastructure/Persistence/Repository/FolderRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Application.Interfaces.Repositories; | ||
using Domain.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Infrastructure.Persistence.Repository; | ||
|
||
public class FolderRepository(DataContext context) : IFolderRepository | ||
{ | ||
public DataContext Context { get; } = context; | ||
|
||
public async Task SaveChangesAsync() | ||
{ | ||
await Context.SaveChangesAsync(); | ||
} | ||
|
||
public async Task<Folder> GetFolderAsync(Guid folderId) | ||
{ | ||
return await Context.Folders | ||
.Where(f => f.FolderId == folderId) | ||
.Include(f => f.Children) | ||
.ThenInclude(f => f.Children) | ||
.ThenInclude(f => f.Children) | ||
.ThenInclude(f => f.Children) | ||
.ThenInclude(f => f.Children) | ||
.FirstOrDefaultAsync(); | ||
} | ||
|
||
public async Task CreateFolderAsync(Folder folder) | ||
{ | ||
await Context.Folders.AddAsync(folder); | ||
} | ||
|
||
public void RemoveFolder(Folder folder) | ||
{ | ||
var allFolders = new List<Folder>(); | ||
GetAllChildren(folder, allFolders); | ||
allFolders.Add(folder); | ||
|
||
Context.Folders.RemoveRange(allFolders); | ||
} | ||
|
||
private void GetAllChildren(Folder folder, List<Folder> allChildren) | ||
{ | ||
allChildren.Add(folder); | ||
foreach (var child in folder.Children) | ||
{ | ||
GetAllChildren(child, allChildren); | ||
} | ||
} | ||
} |
Oops, something went wrong.