|
17 | 17 | using System.Threading.Tasks;
|
18 | 18 | using Flurl.Util;
|
19 | 19 | using ShareBook.Domain.Enums;
|
20 |
| - |
| 20 | +using ShareBook.Domain.Exceptions; |
| 21 | + |
21 | 22 | namespace ShareBook.Api.Controllers
|
22 | 23 | {
|
23 | 24 | [Route("api/[controller]")]
|
@@ -220,6 +221,10 @@ public PagedList<BookVM> ByCategoryId(Guid categoryId, int page, int items)
|
220 | 221 | [ProducesResponseType(typeof(Result), 200)]
|
221 | 222 | public IActionResult RequestBook([FromBody] RequestBookVM requestBookVM)
|
222 | 223 | {
|
| 224 | + User user = GetUser(); |
| 225 | + if (_IsDonator(requestBookVM.BookId, user) && !_IsAdmin(user)) //Permitido solicitar o próprio livro somente para Admin |
| 226 | + throw new ShareBookException("Não é possivel solicitar esse livro pois você é o doador."); |
| 227 | + |
223 | 228 | _bookUserService.Insert(requestBookVM.BookId, requestBookVM.Reason);
|
224 | 229 | return Ok(new Result { SuccessMessage = "Pedido realizado com sucesso!" });
|
225 | 230 | }
|
@@ -389,21 +394,36 @@ public IActionResult RenewChooseDate(Guid bookId)
|
389 | 394 | // apenas doador e adm
|
390 | 395 | private bool _IsBookOwner(Guid bookId)
|
391 | 396 | {
|
392 |
| - var userId = new Guid(Thread.CurrentPrincipal?.Identity?.Name); |
393 |
| - var user = _userService.Find(userId); |
| 397 | + User user = GetUser(); |
394 | 398 | if (user == null)
|
395 | 399 | return false;
|
396 | 400 |
|
397 | 401 | // Adm
|
398 |
| - if (user.Profile == Domain.Enums.Profile.Administrator) |
399 |
| - return true; |
| 402 | + if (_IsAdmin(user)) return true; |
400 | 403 |
|
401 | 404 | // Doador
|
402 |
| - var book = _service.GetBookWithAllUsers(bookId); |
403 |
| - if (book.UserId == userId) |
404 |
| - return true; |
| 405 | + return _IsDonator(bookId, user); |
| 406 | + } |
405 | 407 |
|
406 |
| - return false; |
| 408 | + private bool _IsDonator(Guid bookId, User user) |
| 409 | + { |
| 410 | + if (user == null || user.Id == Guid.Empty) return false; |
| 411 | + Book book = _service.GetBookWithAllUsers(bookId); |
| 412 | + if (book == null || book.Id == Guid.Empty) return false; |
| 413 | + |
| 414 | + return book.UserId == user.Id; |
| 415 | + } |
| 416 | + |
| 417 | + private User GetUser() |
| 418 | + { |
| 419 | + var userId = new Guid(Thread.CurrentPrincipal?.Identity?.Name); |
| 420 | + return _userService.Find(userId); |
| 421 | + } |
| 422 | + |
| 423 | + private bool _IsAdmin(User user) |
| 424 | + { |
| 425 | + if (user == null || user?.Profile == null) return false; |
| 426 | + return user.Profile.Equals(Domain.Enums.Profile.Administrator); |
407 | 427 | }
|
408 | 428 |
|
409 | 429 | // doador, adm e ganhador
|
|
0 commit comments