From c3d4ccae57e037fd3c57c78af1cf277a16f1bf7a Mon Sep 17 00:00:00 2001 From: Szymon Baran Date: Wed, 11 May 2022 18:32:27 +0200 Subject: [PATCH] UserDirectory adding, bugfixes --- .../Abstraction/IDirectoryService.cs | 1 + .../ViewModels/AddDirectoryVM.cs | 3 +- .../ViewModels/DirectorySelectBoxVM.cs | 11 +++ .../Services/DirectoryService.cs | 20 +++- .../Abstraction/IDirectoryRepository.cs | 3 +- .../Repositories/DirectoryRepository.cs | 12 ++- .../src/components/DirectoryAddForm.vue | 91 +++++++++++++++++++ CloudDrive.Vue/src/pages/DrivePage.vue | 50 +++++++++- CloudDrive.Vue/src/stores/directory.js | 45 +++++++++ .../Controllers/FileController.cs | 16 ++++ Hubs/Hubs.csproj | 9 -- 11 files changed, 245 insertions(+), 16 deletions(-) create mode 100644 CloudDrive.Application.Abstraction/ViewModels/DirectorySelectBoxVM.cs create mode 100644 CloudDrive.Vue/src/components/DirectoryAddForm.vue create mode 100644 CloudDrive.Vue/src/stores/directory.js delete mode 100644 Hubs/Hubs.csproj diff --git a/CloudDrive.Application.Abstraction/Abstraction/IDirectoryService.cs b/CloudDrive.Application.Abstraction/Abstraction/IDirectoryService.cs index 9d62d0f..851c2ac 100644 --- a/CloudDrive.Application.Abstraction/Abstraction/IDirectoryService.cs +++ b/CloudDrive.Application.Abstraction/Abstraction/IDirectoryService.cs @@ -5,5 +5,6 @@ namespace CloudDrive.Application public interface IDirectoryService { Task AddDirectory(AddDirectoryVM model, string username); + Task> GetDirectoriesToSelectList(string username); } } diff --git a/CloudDrive.Application.Abstraction/ViewModels/AddDirectoryVM.cs b/CloudDrive.Application.Abstraction/ViewModels/AddDirectoryVM.cs index 90bedc5..0a73233 100644 --- a/CloudDrive.Application.Abstraction/ViewModels/AddDirectoryVM.cs +++ b/CloudDrive.Application.Abstraction/ViewModels/AddDirectoryVM.cs @@ -3,7 +3,8 @@ public class AddDirectoryVM { public string Name { get; set; } - public string UserChosenPath { get; set; } + public string GeneratedPath { get; set; } public int? UserId { get; set; } + public Guid? ParentDirectoryId { get; set; } } } diff --git a/CloudDrive.Application.Abstraction/ViewModels/DirectorySelectBoxVM.cs b/CloudDrive.Application.Abstraction/ViewModels/DirectorySelectBoxVM.cs new file mode 100644 index 0000000..18f4d48 --- /dev/null +++ b/CloudDrive.Application.Abstraction/ViewModels/DirectorySelectBoxVM.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.Http; + +namespace CloudDrive.Application +{ + public class DirectorySelectBoxVM + { + public string Text { get; set; } + public Guid Value { get; set; } + public Guid? ParentDirectoryId { get; set; } + } +} diff --git a/CloudDrive.Application.Services/Services/DirectoryService.cs b/CloudDrive.Application.Services/Services/DirectoryService.cs index 817b820..5d5f180 100644 --- a/CloudDrive.Application.Services/Services/DirectoryService.cs +++ b/CloudDrive.Application.Services/Services/DirectoryService.cs @@ -38,13 +38,21 @@ private string CreateDirectoryOnServer(string userChosenPath) public async Task AddDirectory(AddDirectoryVM model, string username) { - model.UserChosenPath = Regex.Replace($"{username}\\{model.UserChosenPath}\\{model.Name}", "\\\\", "\\"); + if (model.ParentDirectoryId.HasValue) + { + UserDirectory parentDirectory = _directoryRepository.FirstOrDefault(x => x.Id == model.ParentDirectoryId) ?? throw new Exception("Wybrany folder nadrzedny nie istnieje"); + model.GeneratedPath = Path.Combine(parentDirectory.RelativePath, model.Name); + } + else + { + model.GeneratedPath = Path.Combine(username, model.Name); + } AppUser user = _userRepository.FirstOrDefault(x => x.Username == username) ?? throw new Exception("Użytkownik nie istnieje"); model.UserId = user.Id; - if (_directoryRepository.IsDirectoryUnique(model.UserChosenPath)) + if (_directoryRepository.IsDirectoryUnique(model.GeneratedPath)) { - CreateDirectoryOnServer(model.UserChosenPath); + CreateDirectoryOnServer(model.GeneratedPath); await _directoryRepository.AddDirectory(model); } else @@ -53,5 +61,11 @@ public async Task AddDirectory(AddDirectoryVM model, string username) } } + public async Task> GetDirectoriesToSelectList(string username) + { + AppUser user = _userRepository.FirstOrDefault(x => x.Username == username) ?? throw new Exception("Użytkownik nie istnieje"); + return await _directoryRepository.GetDirectoriesToSelectList(user.Id); + } + } } diff --git a/CloudDrive.Data.Abstraction/Abstraction/IDirectoryRepository.cs b/CloudDrive.Data.Abstraction/Abstraction/IDirectoryRepository.cs index aa18a90..70ec15f 100644 --- a/CloudDrive.Data.Abstraction/Abstraction/IDirectoryRepository.cs +++ b/CloudDrive.Data.Abstraction/Abstraction/IDirectoryRepository.cs @@ -3,9 +3,10 @@ namespace CloudDrive.Data.Abstraction { - public interface IDirectoryRepository + public interface IDirectoryRepository : IGenericRepository { Task AddDirectory(AddDirectoryVM model); bool IsDirectoryUnique(string path); + Task> GetDirectoriesToSelectList(int userId); } } diff --git a/CloudDrive.Data.Repositories/Repositories/DirectoryRepository.cs b/CloudDrive.Data.Repositories/Repositories/DirectoryRepository.cs index 646fa8b..5db5245 100644 --- a/CloudDrive.Data.Repositories/Repositories/DirectoryRepository.cs +++ b/CloudDrive.Data.Repositories/Repositories/DirectoryRepository.cs @@ -18,7 +18,7 @@ public async Task AddDirectory(AddDirectoryVM model) await _context.UserDirectories.AddAsync(new UserDirectory { Name = model.Name, - RelativePath = model.UserChosenPath, + RelativePath = model.GeneratedPath, UserId = model.UserId.Value }); @@ -29,5 +29,15 @@ public bool IsDirectoryUnique(string path) { return !_context.UserDirectories.Any(x => x.RelativePath == path); } + + public async Task> GetDirectoriesToSelectList(int userId) + { + return await _context.UserDirectories.Where(x => x.UserId == userId).Select(x => new DirectorySelectBoxVM + { + Text = x.Name, + Value = x.Id, + ParentDirectoryId = x.ParentDirectoryId + }).ToListAsync(); + } } } diff --git a/CloudDrive.Vue/src/components/DirectoryAddForm.vue b/CloudDrive.Vue/src/components/DirectoryAddForm.vue new file mode 100644 index 0000000..92ca058 --- /dev/null +++ b/CloudDrive.Vue/src/components/DirectoryAddForm.vue @@ -0,0 +1,91 @@ + + + + diff --git a/CloudDrive.Vue/src/pages/DrivePage.vue b/CloudDrive.Vue/src/pages/DrivePage.vue index b51f2f1..e6328b8 100644 --- a/CloudDrive.Vue/src/pages/DrivePage.vue +++ b/CloudDrive.Vue/src/pages/DrivePage.vue @@ -5,18 +5,66 @@ leave-active-class="animated fadeOut" > - + +
+ +
+ +
+
+ + + +
diff --git a/CloudDrive.Vue/src/stores/directory.js b/CloudDrive.Vue/src/stores/directory.js new file mode 100644 index 0000000..49c3a37 --- /dev/null +++ b/CloudDrive.Vue/src/stores/directory.js @@ -0,0 +1,45 @@ +import { defineStore } from "pinia"; +import { api } from "src/boot/axios"; +import { useAuthenticationStore } from "./authentication"; +const authenticationStore = useAuthenticationStore(); + +export const useDirectoryStore = defineStore({ + id: "directory", + state: () => ({ + form: { + Name: "", + ParentDirectoryId: "", + }, + }), + actions: { + async addDirectory() { + await api.post("/File/addDirectory", this.form, { + headers: { + Authorization: `Bearer ${authenticationStore.currentUser.token}`, + }, + }); + }, + clearForm() { + this.form.Name = ""; + this.form.ParentDirectoryId = ""; + }, + getDirectoriesToSelectList() { + if ( + authenticationStore.currentUser && + authenticationStore.currentUser.token + ) { + return new Promise((resolve) => { + api + .get("/File/getDirectoriesToSelectList", { + headers: { + Authorization: `Bearer ${authenticationStore.currentUser.token}`, + }, + }) + .then((response) => { + resolve(response.data); + }); + }); + } + }, + }, +}); diff --git a/CloudDrive.WebAPI/Controllers/FileController.cs b/CloudDrive.WebAPI/Controllers/FileController.cs index df3e7e0..df1a0da 100644 --- a/CloudDrive.WebAPI/Controllers/FileController.cs +++ b/CloudDrive.WebAPI/Controllers/FileController.cs @@ -64,6 +64,22 @@ public async Task AddDirectory(AddDirectoryVM model) return Ok(); } + + [Authorize] + [HttpGet("getDirectoriesToSelectList")] + public async Task GetDirectoriesToSelectList() + { + var loggedUsername = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; + + if (loggedUsername == null) + { + return NotFound("Błąd przy próbie znalezienia użytkownika"); + } + + List list = await _directoryService.GetDirectoriesToSelectList(loggedUsername); + + return Ok(list); + } [Authorize] [HttpGet("downloadFile")] diff --git a/Hubs/Hubs.csproj b/Hubs/Hubs.csproj deleted file mode 100644 index 132c02c..0000000 --- a/Hubs/Hubs.csproj +++ /dev/null @@ -1,9 +0,0 @@ - - - - net6.0 - enable - enable - - -