-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from dotnetgoo/wwrootBranch
UserAsset DTOs are done, Repositories and services also. And there is work with wwroot.
- Loading branch information
Showing
13 changed files
with
258 additions
and
3 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,7 @@ | ||
using Shamsheer.Domain.Entities.Assets; | ||
|
||
namespace Shamsheer.Data.IRepositories; | ||
|
||
public interface IUserAssetRepository : IRepository<UserAsset> | ||
{ | ||
} |
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,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) | ||
{ | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/Shamsheer.Service/DTOs/UserAssets/UserAssetForCreationDto.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,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; } | ||
} |
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
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; } | ||
} |
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
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" | ||
}; | ||
} | ||
} |
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,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
13
src/Shamsheer.Service/Interfaces/UserAssets/IUserAssetService.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,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); | ||
} |
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,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
100
src/Shamsheer.Service/Services/UserAssets/UserAssetService.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,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; | ||
} | ||
} |
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