9
9
using Microsoft . AspNetCore . Http ;
10
10
using Microsoft . AspNetCore . Identity ;
11
11
using Microsoft . AspNetCore . Mvc ;
12
+ using Microsoft . Extensions . Localization ;
12
13
using System . Security . Claims ;
13
14
using System . Xml . Linq ;
14
15
@@ -19,24 +20,44 @@ public class AlbumController : Controller
19
20
{
20
21
private readonly IAlbumService _albumService ;
21
22
private readonly UserManager < User > _userManager ;
23
+ private readonly IStringLocalizer < AlbumController > _localizer ;
22
24
23
- public AlbumController ( IAlbumService albumService , UserManager < User > userManager )
25
+ public AlbumController ( IAlbumService albumService , UserManager < User > userManager ,
26
+ IStringLocalizer < AlbumController > localizer )
24
27
{
25
28
_albumService = albumService ;
26
29
_userManager = userManager ;
30
+ _localizer = localizer ;
27
31
}
28
32
29
33
public async Task < ActionResult > Index ( )
30
34
{
31
- var albums = await _albumService . GetAllAlbumsDTO ( _userManager . GetUserId ( User ) ) ;
32
- return View ( albums ) ;
35
+ try
36
+ {
37
+ var albums = await _albumService . GetAllAlbumsDTO ( _userManager . GetUserId ( User ) ) ;
38
+ return View ( albums ) ;
39
+ }
40
+ catch
41
+ {
42
+ TempData [ "error" ] = Convert . ToString ( _localizer [ "Error getting albums" ] ) ;
43
+ return RedirectToAction ( "Index" , "Home" ) ;
44
+ }
45
+
33
46
}
34
47
35
48
[ ActionName ( "Get" ) ]
36
- public async Task < ActionResult > GetAlbum ( int id )
49
+ public async Task < ActionResult > GetAlbum ( int ? id )
37
50
{
38
- var album = await _albumService . GetAlbumById ( id ) ;
39
- return View ( album ) ;
51
+ try
52
+ {
53
+ var album = await _albumService . GetAlbumById ( id ) ;
54
+ return View ( album ) ;
55
+ }
56
+ catch
57
+ {
58
+ TempData [ "error" ] = Convert . ToString ( _localizer [ "Error getting album" ] ) ;
59
+ return RedirectToAction ( "Index" ) ;
60
+ }
40
61
}
41
62
42
63
public ActionResult Create ( )
@@ -45,29 +66,64 @@ public ActionResult Create()
45
66
}
46
67
47
68
[ HttpPost ]
48
- public async Task < ActionResult > Create ( AlbumCreating album )
69
+ public async Task < ActionResult > Create ( AlbumCreating ? album )
49
70
{
50
- var albumId = await _albumService . AddAlbum ( album , _userManager . GetUserId ( User ) ) ;
51
- return RedirectToAction ( "Create" , "AlbumElement" , new { id = albumId } ) ;
71
+ try
72
+ {
73
+ var albumId = await _albumService . AddAlbum ( album , _userManager . GetUserId ( User ) ) ;
74
+ TempData [ "success" ] = Convert . ToString ( _localizer [ "Album successfully created" ] ) ;
75
+ return RedirectToAction ( "Create" , "AlbumElement" , new { id = albumId } ) ;
76
+ }
77
+ catch
78
+ {
79
+ TempData [ "error" ] = Convert . ToString ( _localizer [ "Error creating album" ] ) ;
80
+ return RedirectToAction ( "Create" , "Album" ) ;
81
+ }
52
82
}
53
83
54
- public async Task < ActionResult > Edit ( int id )
84
+ public async Task < ActionResult > Edit ( int ? id )
55
85
{
56
- var album = await _albumService . GetAlbumForEdit ( id , _userManager . GetUserId ( User ) ) ;
57
- return View ( album ) ;
86
+ try
87
+ {
88
+ var album = await _albumService . GetAlbumForEdit ( id , _userManager . GetUserId ( User ) ) ;
89
+ return View ( album ) ;
90
+ }
91
+ catch
92
+ {
93
+ TempData [ "error" ] = Convert . ToString ( _localizer [ "Error getting album" ] ) ;
94
+ return RedirectToAction ( "Index" , "Home" ) ;
95
+ }
58
96
}
59
97
60
98
[ HttpPost ]
61
- public async Task < ActionResult > Edit ( AlbumEditDTO album )
99
+ public async Task < ActionResult > Edit ( AlbumEditDTO ? album )
62
100
{
63
- await _albumService . UpdateAlbum ( album ) ;
64
- return RedirectToAction ( "Index" ) ;
101
+ try
102
+ {
103
+ await _albumService . UpdateAlbum ( album ) ;
104
+ TempData [ "success" ] = Convert . ToString ( _localizer [ "Album successfully updated" ] ) ;
105
+ return RedirectToAction ( "Index" ) ;
106
+ }
107
+ catch
108
+ {
109
+ TempData [ "error" ] = Convert . ToString ( _localizer [ "Error updating album" ] ) ;
110
+ return RedirectToAction ( "Index" ) ;
111
+ }
65
112
}
66
113
67
- public async Task < ActionResult > Delete ( int id )
114
+ public async Task < ActionResult > Delete ( int ? id )
68
115
{
69
- await _albumService . DeleteAlbum ( id , _userManager . GetUserId ( User ) ) ;
70
- return RedirectToAction ( "Index" ) ;
116
+ try
117
+ {
118
+ await _albumService . DeleteAlbum ( id , _userManager . GetUserId ( User ) ) ;
119
+ TempData [ "success" ] = Convert . ToString ( _localizer [ "Album successfully deleted" ] ) ;
120
+ return RedirectToAction ( "Index" ) ;
121
+ }
122
+ catch
123
+ {
124
+ TempData [ "error" ] = Convert . ToString ( _localizer [ "Error deleting album" ] ) ;
125
+ return RedirectToAction ( "Index" ) ;
126
+ }
71
127
}
72
128
73
129
}
0 commit comments