Skip to content

Commit 7874c11

Browse files
committed
Add album element edit and deleted methods, add related views, set other logic
1 parent 5e9867f commit 7874c11

File tree

10 files changed

+194
-23
lines changed

10 files changed

+194
-23
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CoinyProject.Application.DTO;
22
using CoinyProject.Core.Domain.Entities;
3+
using Microsoft.AspNetCore.Http;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -12,10 +13,14 @@ public interface IAlbumService
1213
{
1314
Task<int> AddAlbum(AlbumCreating album);
1415
Task AddAlbumElement(AlbumElementCreating albumElement);
16+
Task<string> ConvertToImageUrl(IFormFile image);
1517
Task<IEnumerable<AlbumGetDTO>> GetAllAlbumsDTO();
1618
Task<AlbumGetByIdDTO> GetAlbumById(int id);
1719
Task<AlbumEditDTO> GetAlbumForEdit(int id);
1820
Task UpdateAlbum(AlbumEditDTO album);
1921
Task DeleteAlbum(int id);
22+
Task<AlbumElementEditDTO> GetAlbumElementForEdit(int id);
23+
Task<int> UpdateAlbumElement(AlbumElementEditDTO album);
24+
Task<int> DeleteAlbumElement(int id);
2025
}
2126
}

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

+68-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using CoinyProject.Infrastructure.Data.Migrations;
66
using CoinyProject.Infrastructure.Data.Repositories;
77
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.AspNetCore.Http;
89
using Microsoft.AspNetCore.Mvc;
910
using Microsoft.EntityFrameworkCore;
1011
using System;
@@ -40,32 +41,34 @@ public async Task<int> AddAlbum(AlbumCreating album)
4041

4142
return _album.Id;
4243
}
43-
public async Task AddAlbumElement(AlbumElementCreating element)
44+
45+
public async Task<string> ConvertToImageUrl(IFormFile image)
4446
{
45-
string imageURL = "";
46-
var album = _unitOfWork.AlbumRepository.Include(x => x.Elements)
47-
.Where(x => x.Id == element.AlbumId)
48-
.FirstOrDefault();
47+
string folder = "albums/elements/";
48+
folder += Guid.NewGuid().ToString() + "_" + image.FileName;
4949

50-
if (element.Image != null)
51-
{
52-
string folder = "albums/elements/";
53-
folder += Guid.NewGuid().ToString() + "_" + element.Image.FileName;
50+
string imageURL = "/" + folder;
51+
52+
string serverFolder = Path.Combine(_webHostEnvironment.WebRootPath, folder);
5453

55-
imageURL = "/" + folder;
54+
await image.CopyToAsync(new FileStream(serverFolder, FileMode.Create));
5655

57-
string serverFolder = Path.Combine(_webHostEnvironment.WebRootPath, folder);
56+
return imageURL;
57+
}
5858

59-
await element.Image.CopyToAsync(new FileStream(serverFolder, FileMode.Create));
60-
}
59+
public async Task AddAlbumElement(AlbumElementCreating element)
60+
{
61+
var album = _unitOfWork.AlbumRepository.Include(x => x.Elements)
62+
.Where(x => x.Id == element.AlbumId)
63+
.FirstOrDefault();
6164

6265
if (album != null)
6366
{
6467
AlbumElement _albumElement = new AlbumElement()
6568
{
6669
Name = element.Name,
6770
Description = element.Description,
68-
ImageURL = imageURL
71+
ImageURL = await ConvertToImageUrl(element.Image),
6972
};
7073

7174
album.Elements.Add(_albumElement);
@@ -162,5 +165,56 @@ public async Task DeleteAlbum(int id)
162165
await _unitOfWork.AlbumRepository.Remove(album);
163166
_unitOfWork.Commit();
164167
}
168+
169+
public async Task<AlbumElementEditDTO> GetAlbumElementForEdit(int id)
170+
{
171+
var albumElement = await _unitOfWork.AlbumElementRepository
172+
.Where(x => x.Id == id)
173+
.AsNoTracking()
174+
.FirstOrDefaultAsync();
175+
176+
var albumElementGetDTO = new AlbumElementEditDTO()
177+
{
178+
Id = albumElement.Id,
179+
Name = albumElement.Name,
180+
Description = albumElement.Description,
181+
ImageURL = albumElement.ImageURL
182+
};
183+
184+
return albumElementGetDTO;
185+
186+
}
187+
188+
public async Task<int> UpdateAlbumElement(AlbumElementEditDTO element)
189+
{
190+
var _element = await _unitOfWork.AlbumElementRepository
191+
.Where(x => x.Id == element.Id)
192+
.FirstOrDefaultAsync();
193+
194+
_element.Name = element.Name;
195+
_element.Description = element.Description;
196+
197+
if(element.Image != null)
198+
_element.ImageURL = await ConvertToImageUrl(element.Image);
199+
200+
await _unitOfWork.AlbumElementRepository.Update(_element);
201+
_unitOfWork.Commit();
202+
203+
return _element.AlbumId;
204+
}
205+
206+
public async Task<int> DeleteAlbumElement(int id)
207+
{
208+
var element = _unitOfWork.AlbumElementRepository
209+
.Where(x => x.Id == id)
210+
.FirstOrDefault();
211+
212+
await _unitOfWork.AlbumElementRepository.Remove(element);
213+
_unitOfWork.Commit();
214+
215+
return element.AlbumId;
216+
}
217+
218+
165219
}
166220
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.AspNetCore.Http;
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 AlbumElementEditDTO
11+
{
12+
public int Id { get; set; }
13+
public string Name { get; set; }
14+
public string? Description { get; set; }
15+
public IFormFile? Image { get; set; }
16+
public string ImageURL { get; set; }
17+
}
18+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class AlbumElement
1414
public int Id { get; set; }
1515
public string Name { get; set; }
1616
public string? Description { get; set; }
17-
public string? ImageURL { get; set; }
18-
public int? AlbumId { get; set; }
17+
public string ImageURL { get; set; }
18+
public int AlbumId { get; set; }
1919

2020
public virtual Album? Album { get; set; }
2121
public virtual Auction? Auction { get; set; }

src/CoinyProject.WebUI/Controllers/AlbumController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task<ActionResult> Index()
2626
return View(albums);
2727
}
2828

29-
[Route("album-details/{id:int:min(1)}", Name = "albumDetailsRoute"), ActionName("Get")]
29+
[ActionName("Get")]
3030
public async Task<ActionResult> GetAlbum(int id)
3131
{
3232
var album = await _albumService.GetAlbumById(id);

src/CoinyProject.WebUI/Controllers/AlbumElementController.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,28 @@ public async Task<IActionResult> Create(AlbumElementCreating element)
3434

3535
return RedirectToAction("Create");
3636
}
37-
37+
38+
public async Task<ActionResult> Edit(int id)
39+
{
40+
var album = await _albumService.GetAlbumElementForEdit(id);
41+
return View(album);
42+
}
43+
44+
[HttpPost]
45+
public async Task<ActionResult> Edit(AlbumElementEditDTO album)
46+
{
47+
var id = await _albumService.UpdateAlbumElement(album);
48+
TempData["success"] = "Album element successfuly updated";
49+
return RedirectToAction("Get","Album", new { id });
50+
}
51+
52+
public async Task<ActionResult> Delete(int id)
53+
{
54+
var albumId = await _albumService.DeleteAlbumElement(id);
55+
TempData["success"] = "Album element successfuly deleted";
56+
return RedirectToAction("Get", "Album", new { id = albumId });
57+
}
58+
3859

3960
}
4061
}

src/CoinyProject.WebUI/Views/Album/Get.cshtml

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@
1010
<p class="text-primary">@(string.IsNullOrEmpty(Model.Description) ? "Description is not available" : Model.Description)</p>
1111
</div>
1212
<div class="col-6 text-end">
13-
<div>
13+
<div class="row-cols-3">
1414
<a asp-controller="Album" asp-action="Index" class="btn btn-primary">
1515
<i class="bi bi-arrow-left"></i> Go Back
1616
</a>
1717
</div>
18-
<div class="pt-4">
19-
<a asp-controller="Album" asp-action="Edit" asp-route-id="@Model.Id" class="btn btn-warning">
18+
<div class="pt-3 ">
19+
<a asp-controller="AlbumElement" asp-action="Create" asp-route-id="@Model.Id" class="btn btn-info">
20+
<i class="bi bi-plus-circle"></i> Add Element
21+
</a>
22+
<a asp-controller="Album" asp-action="Edit" asp-route-id="@Model.Id" class="row-cols-5 btn btn-warning">
2023
<i class="bi bi-pencil"></i> Edit
2124
</a>
22-
<a asp-controller="Album" asp-action="Delete" asp-route-id="@Model.Id" class="btn btn-danger">
25+
<a asp-controller="Album" asp-action="Delete" asp-route-id="@Model.Id" class=" row-cols-5 btn btn-danger">
2326
<i class="bi bi-trash"></i> Delete
2427
</a>
28+
2529
</div>
2630
</div>
2731

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
@using CoinyProject.Application.DTO
2+
@model AlbumElementEditDTO
3+
4+
<form method="post" enctype="multipart/form-data" action="Edit">
5+
<div class="border p-3 mt-4">
6+
<div class="row pb-2">
7+
<h2 class="text-primary">Create Album Element</h2>
8+
<hr />
9+
<div class="row">
10+
<div class="col-md-8">
11+
<input asp-for="Id" type="hidden" />
12+
<div class="mb-3 row p-1">
13+
<label asp-for="Name" class="p-0"></label>
14+
<input asp-for="Name" class="form-control" />
15+
<span asp-validation-for="Name" class="text-danger"></span>
16+
</div>
17+
<div class="mb-3 row p-1">
18+
<label asp-for="Description" class="p-0"></label>
19+
<input asp-for="Description" class="form-control" />
20+
<span asp-validation-for="Description" class="text-danger"></span>
21+
</div>
22+
<div class="mb-3 row p-1">
23+
<label asp-for="Image" class="p-0"></label>
24+
<input id="imgFile" asp-for="Image" type="file" class="form-control" />
25+
<span asp-validation-for="Image" class="text-danger"></span>
26+
</div>
27+
</div>
28+
<div class="col-md-4 ">
29+
<img id="imgViewer" width="300" height="300" src="@Model.ImageURL" class="rounded mx-auto d-block border-3" />
30+
</div>
31+
</div>
32+
<div class="row">
33+
<div class="col-3 col-md-2">
34+
<button id="btnSave" type="submit" class="btn btn-primary form-control">Update</button>
35+
</div>
36+
<div class="col-3 col-md-2">
37+
<a asp-controller="AlbumElement" asp-action="Delete" asp-route-id="@Model.Id" class="form-control btn btn-danger">
38+
<i class="bi bi-trash"></i> Delete
39+
</a>
40+
</div>
41+
<div class="col-6 col-md-3">
42+
<a asp-controller="Album" asp-action="Index" class="btn btn-outline-secondary form-control text-primary">
43+
Back to List
44+
</a>
45+
</div>
46+
</div>
47+
</div>
48+
</div>
49+
</form>
50+
51+
<script type="text/javascript">
52+
imgFile.onchange = evt => {
53+
const [file] = imgFile.files
54+
if (file) {
55+
imgViewer.src = URL.createObjectURL(file)
56+
}
57+
}
58+
</script>
59+
60+
@section Scripts {
61+
@{
62+
<partial name="_ValidationScriptsPartial" />
63+
}
64+
}

src/CoinyProject.WebUI/Views/Shared/_AlbumElementView.cshtml

+5
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66
<div class="card-body">
77
<h3 class="card-title">@Model.Name</h3>
88
<p class="card-text">@(string.IsNullOrEmpty(Model.Description) ? "Description is not availabe" : Model.Description)</p>
9+
<div class="btn-group">
10+
<a asp-action="Edit" asp-controller="AlbumElement" asp-route-id="@Model.Id" class="btn btn-sm btn-warning">
11+
<i class="bi bi-pencil"></i> Edit Element
12+
</a>
13+
</div>
914
</div>
1015
</div>

src/CoinyProject.WebUI/Views/Shared/_AlbumView.cshtml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<p class="card-text">@(string.IsNullOrEmpty(Model.Description) ? "Description is not availabe": Model.Description)</p>
99
<div class="d-flex justify-content-between align-items-center">
1010
<div class="btn-group">
11-
<a asp-route="albumDetailsRoute" asp-route-id="@Model.Id"
11+
<a asp-action="Get" asp-controller="Album" asp-route-id="@Model.Id"
1212
class="btn btn-sm btn-outline-secondary">View details</a>
1313
</div>
1414
<small class="text-muted">@Model.Rate

0 commit comments

Comments
 (0)