Skip to content

Commit

Permalink
UserDirectory adding, bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
szymon-baran committed May 11, 2022
1 parent 3934633 commit c3d4cca
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace CloudDrive.Application
public interface IDirectoryService
{
Task AddDirectory(AddDirectoryVM model, string username);
Task<List<DirectorySelectBoxVM>> GetDirectoriesToSelectList(string username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
}
20 changes: 17 additions & 3 deletions CloudDrive.Application.Services/Services/DirectoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -53,5 +61,11 @@ public async Task AddDirectory(AddDirectoryVM model, string username)
}
}

public async Task<List<DirectorySelectBoxVM>> GetDirectoriesToSelectList(string username)
{
AppUser user = _userRepository.FirstOrDefault(x => x.Username == username) ?? throw new Exception("Użytkownik nie istnieje");
return await _directoryRepository.GetDirectoriesToSelectList(user.Id);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

namespace CloudDrive.Data.Abstraction
{
public interface IDirectoryRepository
public interface IDirectoryRepository : IGenericRepository<UserDirectory>
{
Task AddDirectory(AddDirectoryVM model);
bool IsDirectoryUnique(string path);
Task<List<DirectorySelectBoxVM>> GetDirectoriesToSelectList(int userId);
}
}
12 changes: 11 additions & 1 deletion CloudDrive.Data.Repositories/Repositories/DirectoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
});

Expand All @@ -29,5 +29,15 @@ public bool IsDirectoryUnique(string path)
{
return !_context.UserDirectories.Any(x => x.RelativePath == path);
}

public async Task<List<DirectorySelectBoxVM>> 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();
}
}
}
91 changes: 91 additions & 0 deletions CloudDrive.Vue/src/components/DirectoryAddForm.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<template>
<q-form class="q-gutter-md" @submit="tryAdd">
<p class="text-h5 q-mt-lg q-mb-xs">Dodawanie nowego folderu</p>
<div class="row q-py-lg">
<div class="column q-mx-md form-field">
<q-input
filled
v-model="form.Name"
label="Nazwa folderu"
lazy-rules
color="secondary"
dark
:rules="[
(val) => (val && val.length > 0) || 'Nazwa folderu jest wymagana!',
]"
dense
/>
</div>
<div class="column q-mx-md form-field">
<q-select
filled
v-model="form.ParentDirectoryId"
label="Folder nadrzędny"
color="secondary"
dark
:options="directories"
option-label="text"
option-value="value"
emit-value
map-options
dense
clearable
/>
</div>
<q-space />
<div class="column q-mx-md">
<q-btn label="Dodaj" text-color="secondary" outline type="submit" />
</div>
</div>
</q-form>
</template>

<script>
import { useDirectoryStore } from "../stores/directory.js";
import { mapActions, mapWritableState } from "pinia";
export default {
name: "DirectoryAddForm",
data() {
return {
directories: [],
};
},
computed: {
...mapWritableState(useDirectoryStore, ["form"]),
},
methods: {
...mapActions(useDirectoryStore, [
"clearForm",
"addDirectory",
"getDirectoriesToSelectList",
]),
async tryAdd() {
await this.addDirectory();
this.$q.notify({
type: "positive",
message: "Folder został dodany pomyślnie!",
});
this.$emit("close");
},
},
mounted() {
this.getDirectoriesToSelectList().then((response) => {
this.directories = response;
});
},
beforeUnmount() {
this.clearForm();
},
};
</script>
<style lang="scss" scoped>
.container-register {
width: 350px;
border-top: 4px solid $secondary;
background-color: $dark;
}
.form-field {
width: 36%;
}
</style>
50 changes: 49 additions & 1 deletion CloudDrive.Vue/src/pages/DrivePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,66 @@
leave-active-class="animated fadeOut"
>
<q-page class="flex justify-center">
<main-container title="Mój dysk"></main-container>
<main-container title="Mój dysk">
<div class="row">
<q-space />
<div class="column q-mx-md">
<q-btn
:label="getButtonLabel()"
icon="fa-solid fa-folder-plus"
color="secondary"
:outline="isAddFolderMenuOpen"
:text-color="getTextColor()"
type="submit"
@click="
() => {
isAddFolderMenuOpen = !isAddFolderMenuOpen;
}
"
/>
</div>
</div>
<transition
appear
enter-active-class="animated fadeIn"
leave-active-class="animated fadeOut"
>
<directory-add-form
v-if="isAddFolderMenuOpen"
@close="
() => {
isAddFolderMenuOpen = false;
}
"
/>
</transition>
</main-container>
</q-page>
</transition>
</template>

<script>
import MainContainer from "../components/MainContainer";
import DirectoryAddForm from "../components/DirectoryAddForm";
export default {
name: "DrivePage",
data() {
return {
isAddFolderMenuOpen: false,
};
},
components: {
MainContainer,
DirectoryAddForm,
},
methods: {
getButtonLabel() {
return this.isAddFolderMenuOpen ? "Zamknij" : "Dodaj folder";
},
getTextColor() {
return this.isAddFolderMenuOpen ? "secondary" : "black";
},
},
};
</script>
45 changes: 45 additions & 0 deletions CloudDrive.Vue/src/stores/directory.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
}
},
},
});
16 changes: 16 additions & 0 deletions CloudDrive.WebAPI/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ public async Task<IActionResult> AddDirectory(AddDirectoryVM model)

return Ok();
}

[Authorize]
[HttpGet("getDirectoriesToSelectList")]
public async Task<IActionResult> GetDirectoriesToSelectList()
{
var loggedUsername = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

if (loggedUsername == null)
{
return NotFound("Błąd przy próbie znalezienia użytkownika");
}

List<DirectorySelectBoxVM> list = await _directoryService.GetDirectoriesToSelectList(loggedUsername);

return Ok(list);
}

[Authorize]
[HttpGet("downloadFile")]
Expand Down
9 changes: 0 additions & 9 deletions Hubs/Hubs.csproj

This file was deleted.

0 comments on commit c3d4cca

Please sign in to comment.