Skip to content

Commit d00f1ad

Browse files
committed
Update the creation of albums with elements, add image previews, and include album views
1 parent 481d791 commit d00f1ad

22 files changed

+961
-62
lines changed

src/CoinyProject.Application/AlbumServices/Interfaces/IAlbumService.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CoinyProject.Application.DTO;
2+
using CoinyProject.Core.Domain.Entities;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -12,5 +13,7 @@ public interface IAlbumService
1213
Task AddAlbum(AlbumCreating album);
1314
Task AddAlbumElement(AlbumElementCreating albumElement);
1415
Task<(string, string)> CommitAlbumCreation();
16+
Task<IEnumerable<AlbumGetDTO>> GetAllAlbumsDTO();
17+
Task<AlbumGetByIdDTO> GetAlbumById(int id);
1518
}
1619
}

src/CoinyProject.Application/AlbumServices/Services/AlbumService.cs

+72-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using CoinyProject.Infrastructure.Data;
55
using CoinyProject.Infrastructure.Data.Migrations;
66
using CoinyProject.Infrastructure.Data.Repositories;
7+
using Microsoft.AspNetCore.Hosting;
78
using Microsoft.AspNetCore.Mvc;
89
using Microsoft.EntityFrameworkCore;
910
using System;
@@ -18,10 +19,12 @@ namespace CoinyProject.Application.AlbumServices.Services
1819
public class AlbumService : IAlbumService
1920
{
2021
private readonly UnitOfWork _unitOfWork;
22+
private readonly IWebHostEnvironment _webHostEnvironment;
2123

22-
public AlbumService(ApplicationDBContext dBContext)
24+
public AlbumService(ApplicationDBContext dBContext, IWebHostEnvironment webHostEnvironment)
2325
{
2426
_unitOfWork = new UnitOfWork(dBContext);
27+
_webHostEnvironment = webHostEnvironment;
2528
}
2629

2730
public async Task AddAlbum(AlbumCreating album)
@@ -35,26 +38,41 @@ public async Task AddAlbum(AlbumCreating album)
3538
await _unitOfWork.AlbumRepository.Add(_album);
3639
_unitOfWork.Commit();
3740
}
38-
public Task AddAlbumElement(AlbumElementCreating element)
41+
public async Task AddAlbumElement(AlbumElementCreating element)
3942
{
43+
string imageURL = null;
4044
var album = _unitOfWork.AlbumRepository.Include(x => x.Elements)
4145
.Where(x => x.Elements.Count == 0)
4246
.OrderByDescending(x => x.Id)
4347
.FirstOrDefault();
4448

49+
if (element.Image != null)
50+
{
51+
string folder = "albums/elements/";
52+
folder += Guid.NewGuid().ToString() + "_" + element.Image.FileName;
53+
54+
imageURL = "/" + folder;
55+
56+
string serverFolder = Path.Combine(_webHostEnvironment.WebRootPath, folder);
57+
58+
await element.Image.CopyToAsync(new FileStream(serverFolder, FileMode.Create));
59+
}
60+
4561
if (album != null)
4662
{
4763
AlbumElement _albumElement = new AlbumElement()
4864
{
49-
Name = album.Name,
50-
Description = album.Description,
65+
Name = element.Name,
66+
Description = element.Description,
67+
ImageURL = imageURL
5168
};
5269

5370
album.Elements.Add(_albumElement);
5471
_unitOfWork.Commit();
5572
}
5673

57-
return Task.CompletedTask;
74+
75+
5876
}
5977

6078
public Task<(string,string)> CommitAlbumCreation()
@@ -69,5 +87,54 @@ public Task AddAlbumElement(AlbumElementCreating element)
6987
else
7088
return Task.FromResult(("success", "Album successfule created"));
7189
}
90+
91+
public async Task<IEnumerable<AlbumGetDTO>> GetAllAlbumsDTO()
92+
{
93+
var alums = await _unitOfWork.AlbumRepository
94+
.Include(x => x.Elements)
95+
.AsNoTracking()
96+
.ToListAsync();
97+
98+
var albumsGetDTOList = new List<AlbumGetDTO>();
99+
100+
foreach(Album album in alums)
101+
{
102+
albumsGetDTOList.Add(new AlbumGetDTO()
103+
{
104+
Id = album.Id,
105+
Name = album.Name,
106+
Description = album.Description,
107+
Rate = album.Rate,
108+
TitleImageURL = album.Elements.FirstOrDefault().ImageURL
109+
});
110+
}
111+
return albumsGetDTOList;
112+
}
113+
114+
public async Task<AlbumGetByIdDTO> GetAlbumById(int id)
115+
{
116+
var album = await _unitOfWork.AlbumRepository
117+
.Where(x => x.Id == id)
118+
.Include(x => x.Elements)
119+
.AsNoTracking()
120+
.FirstOrDefaultAsync();
121+
122+
var AlbumGetByIdDTO = new AlbumGetByIdDTO()
123+
{
124+
Id = album.Id,
125+
Name = album.Name,
126+
Rate = album.Rate,
127+
Description = album.Description,
128+
Elements = album.Elements.Select(x => new AlbumElementGetDTO()
129+
{
130+
Id = x.Id,
131+
Name = x.Name,
132+
Description = x.Description,
133+
ImageURL = x.ImageURL
134+
}).ToList()
135+
};
136+
137+
return AlbumGetByIdDTO;
138+
}
72139
}
73140
}

src/CoinyProject.Application/DTO/AlbumElementCreating.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Microsoft.AspNetCore.Http;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -10,6 +11,6 @@ public class AlbumElementCreating
1011
{
1112
public string Name { get; set; }
1213
public string? Description { get; set; }
13-
public byte[]? Image { get; set; }
14+
public IFormFile? Image { get; set; }
1415
}
1516
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using CoinyProject.Core.Domain.Entities;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace CoinyProject.Application.DTO
9+
{
10+
public class AlbumElementGetDTO
11+
{
12+
public int Id { get; set; }
13+
public string Name { get; set; }
14+
public string? Description { get; set; }
15+
public string ImageURL { get; set; }
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CoinyProject.Application.DTO
8+
{
9+
public class AlbumGetByIdDTO
10+
{
11+
public int Id { get; set; }
12+
public string Name { get; set; }
13+
public string? Description { get; set; }
14+
public int Rate { get; set; }
15+
public ICollection<AlbumElementGetDTO> Elements { get; set; }
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CoinyProject.Application.DTO
8+
{
9+
public class AlbumGetDTO
10+
{
11+
public int Id { get; set; }
12+
public string Name { get; set; }
13+
public string? Description { get; set; }
14+
public int Rate { get; set; }
15+
public string TitleImageURL { get; set; }
16+
}
17+
}

src/CoinyProject.Core.Domain/CoinyProject.Core.Domain.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="2.2.0" />
1011
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.2" />
1112
</ItemGroup>
1213

src/CoinyProject.Core.Domain/Entities/AlbumElement.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Microsoft.AspNetCore.Http;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Reflection.Metadata.Ecma335;
@@ -13,7 +14,7 @@ public class AlbumElement
1314
public int Id { get; set; }
1415
public string Name { get; set; }
1516
public string? Description { get; set; }
16-
public byte[]? Image { get; set; }
17+
public string? ImageURL { get; set; }
1718
public int? AlbumId { get; set; }
1819

1920
public virtual Album? Album { get; set; }

src/CoinyProject.Core.Domain/Interfaces/IBaseRepository.cs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public interface IBaseRepository<TEntity> where TEntity : class
1515
Task Remove(TEntity entity);
1616
Task Update(TEntity entity);
1717
IQueryable<TEntity> Include(Expression<Func<TEntity, object>> navigationProperty);
18+
IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> predicate);
1819

1920
}
2021
}

src/CoinyProject.Infrastructure.Data/EntityTypeConfiguration/AlbumElementConfiguration.cs

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public void Configure(EntityTypeBuilder<AlbumElement> builder)
2121

2222
builder.Property(x => x.Description)
2323
.IsRequired(false);
24+
25+
builder.Property(x => x.ImageURL)
26+
.IsRequired(false);
27+
2428
}
2529
}
2630
}

0 commit comments

Comments
 (0)