aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services
diff options
context:
space:
mode:
Diffstat (limited to 'src/DevHive.Services')
-rw-r--r--src/DevHive.Services/Configurations/Mapping/PostMappings.cs4
-rw-r--r--src/DevHive.Services/DevHive.Services.csproj56
-rw-r--r--src/DevHive.Services/Interfaces/ICloudService.cs13
-rw-r--r--src/DevHive.Services/Models/Cloud/CloudinaryService.cs55
-rw-r--r--src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs4
-rw-r--r--src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs3
-rw-r--r--src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs4
-rw-r--r--src/DevHive.Services/Services/PostService.cs34
8 files changed, 138 insertions, 35 deletions
diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs
index d8dcc84..c7466d9 100644
--- a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs
+++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs
@@ -9,7 +9,7 @@ namespace DevHive.Services.Configurations.Mapping
public PostMappings()
{
CreateMap<CreatePostServiceModel, Post>();
- // .ForMember(dest => dest.Files, src => src.Ignore());
+ // .ForMember(dest => dest.Files, src => src.Ignore());
CreateMap<UpdatePostServiceModel, Post>()
.ForMember(dest => dest.Id, src => src.MapFrom(p => p.PostId))
// .ForMember(dest => dest.Files, src => src.Ignore())
@@ -20,7 +20,7 @@ namespace DevHive.Services.Configurations.Mapping
.ForMember(dest => dest.CreatorFirstName, src => src.Ignore())
.ForMember(dest => dest.CreatorLastName, src => src.Ignore())
.ForMember(dest => dest.CreatorUsername, src => src.Ignore());
- //TODO: Map those here /\
+ //TODO: Map those here /\
}
}
}
diff --git a/src/DevHive.Services/DevHive.Services.csproj b/src/DevHive.Services/DevHive.Services.csproj
index 52f0323..66df209 100644
--- a/src/DevHive.Services/DevHive.Services.csproj
+++ b/src/DevHive.Services/DevHive.Services.csproj
@@ -1,27 +1,29 @@
-<Project Sdk="Microsoft.NET.Sdk">
- <PropertyGroup>
- <TargetFramework>net5.0</TargetFramework>
- </PropertyGroup>
-
- <ItemGroup>
- <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
- <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.1">
- <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
- <PrivateAssets>all</PrivateAssets>
- </PackageReference>
- <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
- <ProjectReference Include="..\DevHive.Data\DevHive.Data.csproj" />
-
- <PackageReference Include="AutoMapper" Version="10.1.1" />
- <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
- </ItemGroup>
-
- <ItemGroup>
- <ProjectReference Include="..\DevHive.Common\DevHive.Common.csproj" />
- </ItemGroup>
-
- <PropertyGroup>
- <EnableNETAnalyzers>true</EnableNETAnalyzers>
- <AnalysisLevel>latest</AnalysisLevel>
- </PropertyGroup>
-</Project>
+<Project Sdk="Microsoft.NET.Sdk">
+ <PropertyGroup>
+ <TargetFramework>net5.0</TargetFramework>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
+ <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.1">
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+ <PrivateAssets>all</PrivateAssets>
+ </PackageReference>
+ <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
+ <ProjectReference Include="..\DevHive.Data\DevHive.Data.csproj" />
+
+ <PackageReference Include="AutoMapper" Version="10.1.1" />
+ <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
+
+ <PackageReference Include="CloudinaryDotNet" Version="1.14.0"/>
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\DevHive.Common\DevHive.Common.csproj" />
+ </ItemGroup>
+
+ <PropertyGroup>
+ <EnableNETAnalyzers>true</EnableNETAnalyzers>
+ <AnalysisLevel>latest</AnalysisLevel>
+ </PropertyGroup>
+</Project>
diff --git a/src/DevHive.Services/Interfaces/ICloudService.cs b/src/DevHive.Services/Interfaces/ICloudService.cs
new file mode 100644
index 0000000..6616444
--- /dev/null
+++ b/src/DevHive.Services/Interfaces/ICloudService.cs
@@ -0,0 +1,13 @@
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Http;
+
+namespace DevHive.Services.Interfaces
+{
+ public interface ICloudService
+ {
+ Task<List<string>> UploadFilesToCloud(List<IFormFile> formFiles);
+
+ Task<bool> RemoveFilesFromCloud(List<string> fileUrls);
+ }
+}
diff --git a/src/DevHive.Services/Models/Cloud/CloudinaryService.cs b/src/DevHive.Services/Models/Cloud/CloudinaryService.cs
new file mode 100644
index 0000000..a9bc9bd
--- /dev/null
+++ b/src/DevHive.Services/Models/Cloud/CloudinaryService.cs
@@ -0,0 +1,55 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Threading.Tasks;
+using CloudinaryDotNet;
+using CloudinaryDotNet.Actions;
+using DevHive.Services.Interfaces;
+using Microsoft.AspNetCore.Http;
+
+namespace DevHive.Services.Services
+{
+ public class CloudinaryService : ICloudService
+ {
+ private readonly Cloudinary _cloudinary;
+
+ public CloudinaryService(string cloudName, string apiKey, string apiSecret)
+ {
+ this._cloudinary = new Cloudinary(new Account(cloudName, apiKey, apiSecret));
+ }
+
+ public async Task<List<string>> UploadFilesToCloud(List<IFormFile> formFiles)
+ {
+ List<string> fileUrls = new();
+ foreach (var formFile in formFiles)
+ {
+ string formFileId = Guid.NewGuid().ToString();
+
+ if (formFile.Length > 0)
+ {
+ using (var ms = new MemoryStream())
+ {
+ formFile.CopyTo(ms);
+ byte[] formBytes = ms.ToArray();
+
+ ImageUploadParams imageUploadParams = new()
+ {
+ File = new FileDescription(formFileId, new MemoryStream(formBytes)),
+ PublicId = formFileId
+ };
+
+ ImageUploadResult uploadResult = await this._cloudinary.UploadAsync(imageUploadParams);
+ fileUrls.Add(uploadResult.Url.AbsoluteUri);
+ }
+ }
+ }
+
+ return fileUrls;
+ }
+
+ public async Task<bool> RemoveFilesFromCloud(List<string> fileUrls)
+ {
+ return true;
+ }
+ }
+}
diff --git a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs
index 36f6351..8676f6c 100644
--- a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs
+++ b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs
@@ -1,4 +1,6 @@
using System;
+using System.Collections.Generic;
+using Microsoft.AspNetCore.Http;
namespace DevHive.Services.Models.Post.Post
{
@@ -8,6 +10,6 @@ namespace DevHive.Services.Models.Post.Post
public string Message { get; set; }
- // public List<IFormFile> Files { get; set; }
+ public List<IFormFile> Files { get; set; }
}
}
diff --git a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs
index 3e673c1..f0a4fe5 100644
--- a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs
+++ b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using DevHive.Services.Models.Post.Comment;
+using Microsoft.Extensions.FileProviders;
namespace DevHive.Services.Models.Post.Post
{
@@ -20,6 +21,6 @@ namespace DevHive.Services.Models.Post.Post
public List<ReadCommentServiceModel> Comments { get; set; } = new();
- //public List<string> Files { get; set; }
+ public List<IFileInfo> Files { get; set; }
}
}
diff --git a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs
index 8924b07..24b0b74 100644
--- a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs
+++ b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs
@@ -1,4 +1,6 @@
using System;
+using System.Collections.Generic;
+using Microsoft.AspNetCore.Http;
namespace DevHive.Services.Models.Post.Post
{
@@ -10,6 +12,6 @@ namespace DevHive.Services.Models.Post.Post
public string NewMessage { get; set; }
- // public List<IFormFile> Files { get; set; }
+ public List<IFormFile> Files { get; set; }
}
}
diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs
index d80d815..7ce7b58 100644
--- a/src/DevHive.Services/Services/PostService.cs
+++ b/src/DevHive.Services/Services/PostService.cs
@@ -15,13 +15,15 @@ namespace DevHive.Services.Services
{
public class PostService : IPostService
{
+ private readonly ICloudService _cloudService;
private readonly IUserRepository _userRepository;
private readonly IPostRepository _postRepository;
private readonly ICommentRepository _commentRepository;
private readonly IMapper _postMapper;
- public PostService(IUserRepository userRepository, IPostRepository postRepository, ICommentRepository commentRepository, IMapper postMapper)
+ public PostService(ICloudService cloudService, IUserRepository userRepository, IPostRepository postRepository, ICommentRepository commentRepository, IMapper postMapper)
{
+ this._cloudService = cloudService;
this._userRepository = userRepository;
this._postRepository = postRepository;
this._commentRepository = commentRepository;
@@ -35,9 +37,12 @@ namespace DevHive.Services.Services
throw new ArgumentException("User does not exist!");
Post post = this._postMapper.Map<Post>(createPostServiceModel);
- post.TimeCreated = DateTime.Now;
+
+ if (createPostServiceModel.Files.Count != 0)
+ post.FileUrls = await _cloudService.UploadFilesToCloud(createPostServiceModel.Files);
post.Creator = await this._userRepository.GetByIdAsync(createPostServiceModel.CreatorId);
+ post.TimeCreated = DateTime.Now;
bool success = await this._postRepository.AddAsync(post);
if (success)
@@ -116,9 +121,23 @@ namespace DevHive.Services.Services
throw new ArgumentException("Post does not exist!");
Post post = this._postMapper.Map<Post>(updatePostServiceModel);
- post.TimeCreated = DateTime.Now;
+
+ if (updatePostServiceModel.Files.Count != 0)
+ {
+ if (await this._postRepository.DoesPostHaveFiles(updatePostServiceModel.PostId))
+ {
+ List<string> fileUrls = await this._postRepository.GetFileUrls(updatePostServiceModel.PostId);
+ bool success = await _cloudService.RemoveFilesFromCloud(fileUrls);
+ if (!success)
+ throw new InvalidCastException("Could not delete files from the post!");
+ }
+
+ post.FileUrls = await _cloudService.UploadFilesToCloud(updatePostServiceModel.Files) ??
+ throw new ArgumentNullException("Unable to upload images to cloud");
+ }
post.Creator = await this._userRepository.GetByIdAsync(updatePostServiceModel.CreatorId);
+ post.TimeCreated = DateTime.Now;
bool result = await this._postRepository.EditAsync(updatePostServiceModel.PostId, post);
@@ -155,6 +174,15 @@ namespace DevHive.Services.Services
throw new ArgumentException("Post does not exist!");
Post post = await this._postRepository.GetByIdAsync(id);
+
+ if (await this._postRepository.DoesPostHaveFiles(id))
+ {
+ List<string> fileUrls = await this._postRepository.GetFileUrls(id);
+ bool success = await _cloudService.RemoveFilesFromCloud(fileUrls);
+ if (!success)
+ throw new InvalidCastException("Could not delete files from the post. Please try again");
+ }
+
return await this._postRepository.DeleteAsync(post);
}