Skip to content

Commit

Permalink
Merge pull request #46 from dotnetgoo/wwrootBranch
Browse files Browse the repository at this point in the history
UserAsset DTOs are done, Repositories and services also. And there is work with wwroot.
  • Loading branch information
dotnetgoo authored Oct 23, 2023
2 parents 0c25faf + 011abbf commit 4ebb3b0
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/Shamsheer.Data/IRepositories/IUserAssetRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Shamsheer.Domain.Entities.Assets;

namespace Shamsheer.Data.IRepositories;

public interface IUserAssetRepository : IRepository<UserAsset>
{
}
12 changes: 12 additions & 0 deletions src/Shamsheer.Data/Repositories/UserAssetRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Shamsheer.Data.DbContexts;
using Shamsheer.Data.IRepositories;
using Shamsheer.Domain.Entities.Assets;

namespace Shamsheer.Data.Repositories;

public class UserAssetRepository : Repository<UserAsset>, IUserAssetRepository
{
public UserAssetRepository(ShamsheerDbContext dbContext) : base(dbContext)
{
}
}
10 changes: 10 additions & 0 deletions src/Shamsheer.Messenger.Api/Shamsheer.Messenger.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@
<ProjectReference Include="..\Shamsheer.Service\Shamsheer.Service.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\Media\Images\" />
<Folder Include="wwwroot\Media\Audios\" />
<Folder Include="wwwroot\Media\Files\" />
<Folder Include="wwwroot\Media\ProfilePictures\Groups\" />
<Folder Include="wwwroot\Media\ProfilePictures\Channels\" />
<Folder Include="wwwroot\Media\ProfilePictures\Users\" />
<Folder Include="wwwroot\Media\Videos\" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions src/Shamsheer.Service/DTOs/UserAssets/UserAssetForCreationDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Shamsheer.Service.DTOs.UserAssets;

public class UserAssetForCreationDto
{
public string Name { get; set; }
public string Path { get; set; }
public string Extension { get; set; }
public long Size { get; set; }
public string Type { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class UserAssetForResultDto
{
public long Id { get; set; }
public long UserId { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public string Extension { get; set; }
Expand Down
6 changes: 5 additions & 1 deletion src/Shamsheer.Service/DTOs/Users/UserForCreationDto.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
namespace Shamsheer.Service.DTOs.Users;
using Shamsheer.Service.DTOs.UserAssets;
using System.Collections.Generic;

namespace Shamsheer.Service.DTOs.Users;

public class UserForCreationDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public UserAssetForCreationDto Assets { get; set; }
}
5 changes: 4 additions & 1 deletion src/Shamsheer.Service/DTOs/Users/UserForUpdateDto.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Shamsheer.Service.DTOs.Users;
using Shamsheer.Service.DTOs.UserAssets;

namespace Shamsheer.Service.DTOs.Users;

public class UserForUpdateDto
{
Expand All @@ -7,4 +9,5 @@ public class UserForUpdateDto
public string Email { get; set; }
public string Phone { get; set; }
public string Username { get; set; }
public UserAssetForCreationDto Assets { get; set; }
}
34 changes: 34 additions & 0 deletions src/Shamsheer.Service/Helpers/MediaHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.IO;
using System;

namespace Shamsheer.Service.Helpers;

public class MediaHelper
{
public static string IMAGE = "Images";
public static string FILE = "Files";
public static string AUDIO = "Audios";
public static string VIDEO = "Videos";
public static string MakeImageName(string filename, string filePath)
{
FileInfo fileInfo = new FileInfo(filename);
string extension = fileInfo.Extension;
string name = "";
if (filePath == IMAGE)
name = "IMG_" + Guid.NewGuid() + extension;
else if (filePath == FILE)
name = "FILE_" + Guid.NewGuid() + extension;
else if (filePath == AUDIO)
name = "AUDIO_" + Guid.NewGuid() + extension;
else if (filePath == VIDEO)
name = "VIDEO_" + Guid.NewGuid() + extension;
return name;
}
public static string[] GetImageExtensions()
{
return new string[]
{
".jpg", ".jpeg", ".png", ".gif", ".bmp", ".heic"
};
}
}
10 changes: 10 additions & 0 deletions src/Shamsheer.Service/Interfaces/Commons/IFileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace Shamsheer.Service.Interfaces.Commons;

public interface IFileService
{
public Task<string> UploadImageAsync(IFormFile image, string rootpath, string filePath);
public Task<bool> DeleteImageAsync(string subpath);
}
13 changes: 13 additions & 0 deletions src/Shamsheer.Service/Interfaces/UserAssets/IUserAssetService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using Shamsheer.Service.DTOs.UserAssets;

namespace Shamsheer.Service.Interfaces.UserAssets;

public interface IUserAssetService
{
Task<bool> RemoveAsync(long id);
Task<UserAssetForResultDto> RetrieveByIdAsync(long userId, long id);
Task<UserAssetForResultDto> CreateAsync(UserAssetForCreationDto dto);
Task<IEnumerable<UserAssetForResultDto>> RetrieveAllAsync(long userId);
}
47 changes: 47 additions & 0 deletions src/Shamsheer.Service/Services/Commons/FileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Shamsheer.Service.Helpers;
using Microsoft.AspNetCore.Hosting;
using Shamsheer.Service.Interfaces.Commons;

namespace Shamsheer.Service.Services.Commons;

public class FileService : IFileService
{
private readonly string MEDIA = "Media";
private readonly string ROOTPATH;

public FileService(IWebHostEnvironment env)
{
ROOTPATH = env.WebRootPath;
}
public async Task<bool> DeleteImageAsync(string subpath)
{
string path = Path.Combine(ROOTPATH, subpath);

if (File.Exists(path))
{
await Task.Run(() =>
{
File.Delete(path);
});

return true;
}

return false;
}

public async Task<string> UploadImageAsync(IFormFile image, string rootpath, string filePath)
{
string newImageName = MediaHelper.MakeImageName(image.FileName, filePath);
string subPath = Path.Combine(MEDIA, filePath, rootpath, newImageName);
string path = Path.Combine(ROOTPATH, subPath);
var stream = new FileStream(path, FileMode.Create);
await image.CopyToAsync(stream);
stream.Close();

return subPath;
}
}
100 changes: 100 additions & 0 deletions src/Shamsheer.Service/Services/UserAssets/UserAssetService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using AutoMapper;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Shamsheer.Service.Exceptions;
using Shamsheer.Data.IRepositories;
using Microsoft.EntityFrameworkCore;
using Shamsheer.Domain.Entities.Assets;
using Shamsheer.Service.DTOs.UserAssets;
using Shamsheer.Service.Interfaces.UserAssets;

namespace Shamsheer.Service.Services.UserAssets;

public class UserAssetService : IUserAssetService
{
private readonly IMapper _mapper;
private readonly IUserRepository _userRepository;
private readonly IUserAssetRepository _userAssetRepository;

public UserAssetService(IMapper mapper, IUserRepository userRepository, IUserAssetRepository userAssetRepository)
{
_mapper = mapper;
_userRepository = userRepository;
_userAssetRepository = userAssetRepository;
}
public async Task<UserAssetForResultDto> CreateAsync(UserAssetForCreationDto dto)
{
//Identify UserId TODO:LOGIC
long userId = 0; //Default qiymat berib turildi, aslida jwt tokendan yechib olinadi
var user = await _userRepository.SelectAll()
.Where(u => u.Id == userId)
.FirstOrDefaultAsync();
if (user is null)
throw new ShamsheerException(404, "User is not found.");

var asset = await _userAssetRepository.SelectAll()
.Where(ua => ua.Path == dto.Path)
.FirstOrDefaultAsync();
if (asset is not null)
throw new ShamsheerException(404, "User Asset is already exist.");

var mappedAsset = _mapper.Map<UserAsset>(dto);
mappedAsset.UserId = userId;
mappedAsset.CreatedAt = DateTime.UtcNow;
var result = await _userAssetRepository.InsertAsync(mappedAsset);

return _mapper.Map<UserAssetForResultDto>(result);
}

public async Task<bool> RemoveAsync(long id)
{
var userAsset = await _userAssetRepository.SelectAll()
.Where(ur => ur.Id == id)
.FirstOrDefaultAsync();

if (userAsset is null)
throw new ShamsheerException(404, "User Asset is not found.");

await _userAssetRepository.DeleteAsync(id);

return true;
}

public async Task<IEnumerable<UserAssetForResultDto>> RetrieveAllAsync(long userId)
{
//TODO:LOGIC pagination logic
var user = await _userRepository.SelectAll()
.Where(u => u.Id == userId)
.FirstOrDefaultAsync();

if (user is null)
throw new ShamsheerException(404, "User is not found.");

var mappedAssets = _mapper.Map<IEnumerable<UserAssetForResultDto>>(user.Assets);

return mappedAssets;
}

public async Task<UserAssetForResultDto> RetrieveByIdAsync(long userId, long id)
{
var user = await _userRepository.SelectAll()
.Where(u => u.Id == userId)
.FirstOrDefaultAsync();

if (user is null)
throw new ShamsheerException(404, "User is not found.");

var asset = await _userAssetRepository.SelectAll()
.Where(u => u.Id == id)
.FirstOrDefaultAsync();

if (asset is null)
throw new ShamsheerException(404, "User Asset is not found.");

var mappedAsset = _mapper.Map<UserAssetForResultDto>(asset);

return mappedAsset;
}
}
6 changes: 5 additions & 1 deletion src/Shamsheer.Service/Shamsheer.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

<ItemGroup>
<Folder Include="Extensions\" />
<Folder Include="Helpers\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.17" />
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Shamsheer.Data\Shamsheer.Data.csproj" />
<ProjectReference Include="..\Shamsheer.Domain\Shamsheer.Domain.csproj" />
Expand Down

0 comments on commit 4ebb3b0

Please sign in to comment.