From 8707af3d8375b47575735d95eb3fae6f3787de40 Mon Sep 17 00:00:00 2001 From: mkoper02 Date: Sat, 22 Jun 2024 12:05:50 +0200 Subject: [PATCH] Image controllers update --- .../ReasnAPI/Controllers/MeController.cs | 99 +++++++++++-------- .../ReasnAPI/ReasnAPI/Services/UserService.cs | 71 +++++++------ 2 files changed, 92 insertions(+), 78 deletions(-) diff --git a/Server/ReasnAPI/ReasnAPI/Controllers/MeController.cs b/Server/ReasnAPI/ReasnAPI/Controllers/MeController.cs index 1f9438cf..e38c5d4e 100644 --- a/Server/ReasnAPI/ReasnAPI/Controllers/MeController.cs +++ b/Server/ReasnAPI/ReasnAPI/Controllers/MeController.cs @@ -34,9 +34,9 @@ public IActionResult UpdateCurrentUser( { validator.ValidateAndThrow(userDto); - var user = _userService.GetCurrentUser(); - - // Non-admin users can't update their role to admin + var user = _userService.GetCurrentUser(); + + // Non-admin users can't update their role to admin if (user.Role != UserRole.Admin && userDto.Role == UserRole.Admin) { return Forbid(); @@ -50,44 +50,59 @@ public IActionResult UpdateCurrentUser( [HttpPost] [Route("image")] [ProducesResponseType(StatusCodes.Status201Created)] - public async Task AddCurrentUserImage([FromForm] IFormFile image) + public async Task AddCurrentUserImage([FromForm] List images) { - using var ms = new MemoryStream(); - await image.CopyToAsync(ms); - var fileBytes = ms.ToArray(); - - var imageDto = new ImageDto - { - ObjectId = _userService.GetCurrentUser().Id, - ObjectType = ObjectType.User, - ImageData = fileBytes - }; - - var createdImage = _imageService.CreateImages([imageDto]); - return Ok(createdImage); + var userId = _userService.GetCurrentUser().Id; + + var imageDtos = new List(); + + foreach (var image in images.Where(image => image.Length > 0)) + { + using var ms = new MemoryStream(); + await image.CopyToAsync(ms); + var fileBytes = ms.ToArray(); + + var imageDto = new ImageDto + { + ObjectId = userId, + ObjectType = ObjectType.User, + ImageData = fileBytes + }; + + _imageService.CreateImages([imageDto]); + imageDtos.Add(imageDto); + } + + return Ok(); } [HttpPut] [Route("image")] [ProducesResponseType(StatusCodes.Status200OK)] - public async Task UpdateCurrentUserImage([FromForm] IFormFile image) + public async Task UpdateCurrentUserImage([FromForm] List images) { var userId = _userService.GetCurrentUser().Id; - using var ms = new MemoryStream(); - await image.CopyToAsync(ms); - var fileBytes = ms.ToArray(); + var imageDtos = new List(); - var imageDto = new ImageDto - { - ObjectId = userId, - ObjectType = ObjectType.User, - ImageData = fileBytes - }; - - _imageService.UpdateImageForUser(userId, imageDto); + foreach (var image in images.Where(image => image.Length > 0)) + { + using var ms = new MemoryStream(); + await image.CopyToAsync(ms); + var fileBytes = ms.ToArray(); + + var imageDto = new ImageDto + { + ObjectId = userId, + ObjectType = ObjectType.User, + ImageData = fileBytes + }; + + _imageService.UpdateImageForUser(userId, imageDto); + imageDtos.Add(imageDto); + } - return Ok(imageDto); + return Ok(); } [HttpDelete] @@ -109,10 +124,10 @@ public IActionResult GetCurrentUserEvents() var user = _userService.GetCurrentUser(); var events = _eventService.GetUserEvents(user.Username); - if (user.Role == UserRole.Organizer) - { - var organizerEvents = _eventService.GetEventsByFilter(e => e.OrganizerId == user.Id); - events = events.Concat(organizerEvents); + if (user.Role == UserRole.Organizer) + { + var organizerEvents = _eventService.GetEventsByFilter(e => e.OrganizerId == user.Id); + events = events.Concat(organizerEvents); } return Ok(events); @@ -153,14 +168,14 @@ public IActionResult ConfirmCurrentUserEventAttendance([FromRoute] string slug) [Route("events/{slug}/cancel")] [ProducesResponseType(StatusCodes.Status204NoContent)] public IActionResult CancelCurrentUserEventAttendance([FromRoute] string slug) - { - var eventId = _eventService.GetEventBySlug(slug).Id; - var userId = _userService.GetCurrentUser().Id; - var participant = _participantService.GetParticipantsByFilter(p => p.EventId == eventId && p.UserId == userId).First(); - - _participantService.DeleteParticipant(participant.UserId); - - return NoContent(); + { + var eventId = _eventService.GetEventBySlug(slug).Id; + var userId = _userService.GetCurrentUser().Id; + var participant = _participantService.GetParticipantsByFilter(p => p.EventId == eventId && p.UserId == userId).First(); + + _participantService.DeleteParticipant(participant.UserId); + + return NoContent(); } [HttpGet] diff --git a/Server/ReasnAPI/ReasnAPI/Services/UserService.cs b/Server/ReasnAPI/ReasnAPI/Services/UserService.cs index becbf7ef..007c56ab 100644 --- a/Server/ReasnAPI/ReasnAPI/Services/UserService.cs +++ b/Server/ReasnAPI/ReasnAPI/Services/UserService.cs @@ -1,55 +1,55 @@ using Microsoft.EntityFrameworkCore; using ReasnAPI.Exceptions; using ReasnAPI.Mappers; -using ReasnAPI.Models.Database; +using ReasnAPI.Models.Database; using ReasnAPI.Models.DTOs; using Serilog; using System.Linq.Expressions; using System.Security.Claims; using System.Transactions; - -namespace ReasnAPI.Services; - -public class UserService -{ - private readonly ReasnContext _context; - private readonly IHttpContextAccessor _httpContextAccessor; - + +namespace ReasnAPI.Services; + +public class UserService +{ + private readonly ReasnContext _context; + private readonly IHttpContextAccessor _httpContextAccessor; + public UserService(ReasnContext context) { _context = context; } - public UserService(ReasnContext context, IHttpContextAccessor httpContextAccessor) - { - _context = context; - _httpContextAccessor = httpContextAccessor; - } - - public User GetCurrentUser() - { + public UserService(ReasnContext context, IHttpContextAccessor httpContextAccessor) + { + _context = context; + _httpContextAccessor = httpContextAccessor; + } + + public User GetCurrentUser() + { var httpContext = _httpContextAccessor.HttpContext; if (httpContext is null) - { - throw new InvalidOperationException("No HTTP context available"); - } - + { + throw new InvalidOperationException("No HTTP context available"); + } + var email = httpContext.User.FindFirstValue(ClaimTypes.Email); - if (string.IsNullOrEmpty(email)) - { - throw new UnauthorizedAccessException("No email claim found in token"); - } - - var user = _context.Users.FirstOrDefault(u => u.Email == email); - - if (user is null) - { - throw new NotFoundException("User associated with email not found"); - } - - return user; - } + if (string.IsNullOrEmpty(email)) + { + throw new UnauthorizedAccessException("No email claim found in token"); + } + + var user = _context.Users.FirstOrDefault(u => u.Email == email); + + if (user is null) + { + throw new NotFoundException("User associated with email not found"); + } + + return user; + } public UserDto UpdateUser(string username, UserDto userDto) { @@ -94,7 +94,6 @@ public UserDto UpdateUser(string username, UserDto userDto) user.Email = userDto.Email; user.Phone = userDto.Phone; user.Role = userDto.Role; - user.AddressId = userDto.AddressId; user.UpdatedAt = DateTime.UtcNow; _context.Users.Update(user);