Skip to content

Commit

Permalink
Image controllers update
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoper02 committed Jun 22, 2024
1 parent 4c9dbdf commit 8707af3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 78 deletions.
99 changes: 57 additions & 42 deletions Server/ReasnAPI/ReasnAPI/Controllers/MeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -50,44 +50,59 @@ public IActionResult UpdateCurrentUser(
[HttpPost]
[Route("image")]
[ProducesResponseType<ImageDto>(StatusCodes.Status201Created)]
public async Task<IActionResult> AddCurrentUserImage([FromForm] IFormFile image)
public async Task<IActionResult> AddCurrentUserImage([FromForm] List<IFormFile> 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<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.CreateImages([imageDto]);
imageDtos.Add(imageDto);
}

return Ok();
}

[HttpPut]
[Route("image")]
[ProducesResponseType<ImageDto>(StatusCodes.Status200OK)]
public async Task<IActionResult> UpdateCurrentUserImage([FromForm] IFormFile image)
public async Task<IActionResult> UpdateCurrentUserImage([FromForm] List<IFormFile> images)
{
var userId = _userService.GetCurrentUser().Id;

using var ms = new MemoryStream();
await image.CopyToAsync(ms);
var fileBytes = ms.ToArray();
var imageDtos = new List<ImageDto>();

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]
Expand All @@ -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);
Expand Down Expand Up @@ -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]
Expand Down
71 changes: 35 additions & 36 deletions Server/ReasnAPI/ReasnAPI/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -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)

Check warning on line 18 in Server/ReasnAPI/ReasnAPI/Services/UserService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (ubuntu-latest)

Non-nullable field '_httpContextAccessor' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 18 in Server/ReasnAPI/ReasnAPI/Services/UserService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (macos-latest)

Non-nullable field '_httpContextAccessor' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 18 in Server/ReasnAPI/ReasnAPI/Services/UserService.cs

View workflow job for this annotation

GitHub Actions / dotnet-tests (windows-latest)

Non-nullable field '_httpContextAccessor' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_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)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8707af3

Please sign in to comment.