Skip to content

Commit

Permalink
PS-108 Upload avatar (#26)
Browse files Browse the repository at this point in the history
* Refactor entities

* Fix typo in column name

* file share upload avatar

* Fix tests

* Fix warnings

* fix all warnings

* removed
  • Loading branch information
MysticFragilist authored Mar 24, 2024
1 parent d525d11 commit ed2ceba
Show file tree
Hide file tree
Showing 42 changed files with 674 additions and 179 deletions.
10 changes: 6 additions & 4 deletions core/Controllers/EventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ public class EventsController(ILogger<EventsController> logger, IEventService ev
/// Get events by date, activity area and tags
/// </summary>
/// <param name="startDate">if null, will get every event until the first element</param>
/// <param name="endDate"></param>
/// <param name="activityAreas"></param>
/// <param name="tags"></param>
/// <returns></returns>
/// <param name="endDate">if null, will get every element unitl infinity</param>
/// <param name="organizerId">Filter by organizerId</param>
/// <param name="activityAreas">Filter by a list of OR applicable activityAreas</param>
/// <param name="tags">Filter by a list of OR applicable tags</param>
/// <param name="pagination">Sort and take only the necessary page</param>
/// <returns>events filtered and sorted</returns>
[HttpGet]
[OutputCache(VaryByQueryKeys = [ "startDate", "endDate", "activityAreas", "tags", "pageNumber", "pageSize" ])]
public ActionResult<IEnumerable<EventResponseDTO>> GetEvents(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using api.core.data.entities;
using api.core.Data;
using api.core.Data;
using api.core.Data.requests;
using api.core.Data.Responses;
using api.core.Misc;
Expand All @@ -12,8 +11,8 @@ namespace api.core.controllers;

[Authorize]
[ApiController]
[Route("api/user")]
public class UserController(IUserService userService, IAuthService authService) : ControllerBase
[Route("api/me")]
public class MeController(IUserService userService) : ControllerBase
{
[HttpGet]
public IActionResult GetUser()
Expand All @@ -34,4 +33,22 @@ public IActionResult UpdateUser([FromBody] UserUpdateDTO user)
var userId = JwtUtils.GetUserIdFromAuthHeader(HttpContext.Request.Headers["Authorization"]!);
return userService.UpdateUser(userId, user) ? Ok() : BadRequest();
}

/// <summary>
/// Update the user connected avatar
/// </summary>
/// <param name="avatarReq"></param>
/// <returns>The url of the downloadable avatar file</returns>
[HttpPatch("avatar")]
public IActionResult UpdateUserAvatar([FromForm] UserAvatarUpdateDTO avatarReq)
{
var userId = JwtUtils.GetUserIdFromAuthHeader(HttpContext.Request.Headers["Authorization"]!);
var url = userService.UpdateUserAvatar(userId, avatarReq.avatarFile);

return new OkObjectResult(
new Response<string>
{
Data = url,
});
}
}
2 changes: 1 addition & 1 deletion core/Controllers/ModeratorEventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace api.core.Controllers;
[ApiController]
[Authorize(Policy = AuthPolicies.IsModerator)]
[Route("api/moderator/events")]
public class ModeratorEventsController(ILogger<ModeratorEventsController> logger, IEventService eventService, IUserService userService, IReportService reportService) : ControllerBase
public class ModeratorEventsController(ILogger<ModeratorEventsController> logger, IEventService eventService, IReportService reportService) : ControllerBase
{
[HttpPatch("{id}/state")]
public IActionResult UpdateEventState(Guid id, [FromQuery] State newState, [FromQuery] string? reason)
Expand Down
8 changes: 5 additions & 3 deletions core/Controllers/ModeratorUserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ public async Task<IActionResult> CreateOrganizer([FromBody] UserCreateDTO organi
Guid.TryParse(supabaseUser, out Guid userId);
var created = userService.AddOrganizer(userId, organizer);
var frontBaseUrl = Environment.GetEnvironmentVariable("FRONTEND_BASE_URL") ?? throw new Exception("FRONTEND_BASE_URL is not set");
await emailService.SendEmailAsync(
await emailService.SendEmailAsync<UserCreationModel>(
organizer.Email,
"Votre compte Hello!",
new UserCreationModel
{
Salutation = $"Bonjour {organizer.Organisation},",
Title = "Création de compte Hello!",
Salutation = $"Bonjour {organizer.Organization},",
AccountCreatedText = "Votre compte Hello a été créé!",
TemporaryPasswordHeader = "Votre mot de passe temporaire est: ",
TemporaryPasswordHeader = "Voici votre mot de passe temporaire, assurez-vous de suivre les indications pour le modifier lors de votre première connexion. ",
TemporaryPassword = strongPassword,
LoginButtonText = "Se connecter",
ButtonLink = new Uri($"{frontBaseUrl}/fr/login")
Expand Down Expand Up @@ -62,6 +63,7 @@ await emailService.SendEmailAsync(
"Votre compte Hello a été désactivé",
new UserDeactivationModel
{
Title = "Désactivation de votre compte Hello",
Salutation = $"Bonjour {organizer.Organisation},",
UserDeactivationHeader = "Votre compte Hello a été désactivé pour la raison suivate: ",
UserDeactivationReason = reason ?? "",
Expand Down
2 changes: 1 addition & 1 deletion core/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace api.core.controllers;

[ApiController]
[Route("api/test")]
public class TestController(IEmailService service, IConfiguration configuration) : ControllerBase
public class TestController(IConfiguration configuration) : ControllerBase
{

[HttpPost("login")]
Expand Down
18 changes: 18 additions & 0 deletions core/Data/Entities/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace api.core.Data.Entities;

public class BaseEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }

public DateTime CreatedAt { get; set; }

public DateTime UpdatedAt { get; set; }

public DateTime? DeletedAt { get; set; }

}
15 changes: 3 additions & 12 deletions core/Data/Entities/Moderator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,15 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

using api.core.Data.Entities;

using Microsoft.EntityFrameworkCore;

namespace api.core.data.entities;

[Table("Moderator")]
public partial class Moderator
public partial class Moderator : User
{
[Key]
public Guid Id { get; set; }

public string Email { get; set; } = null!;

public DateTime CreatedAt { get; set; }

public DateTime UpdatedAt { get; set; }

public DateTime? DeletedAt { get; set; }

[InverseProperty("Moderator")]
public virtual ICollection<Publication> Publications { get; set; } = new List<Publication>();
}
17 changes: 4 additions & 13 deletions core/Data/Entities/Organizer.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

using api.core.Data.Entities;

namespace api.core.data.entities;

[Table("Organizer")]
public partial class Organizer
public partial class Organizer : User
{
[Key]
public Guid Id { get; set; }

public string Email { get; set; } = null!;

public string Organisation { get; set; } = null!;
public string Organization { get; set; } = null!;

public string ActivityArea { get; set; } = null!;

Expand All @@ -35,12 +32,6 @@ public partial class Organizer

public string? WebSiteLink { get; set; }

public DateTime CreatedAt { get; set; }

public DateTime UpdatedAt { get; set; }

public DateTime? DeletedAt { get; set; }

[InverseProperty("Organizer")]
public virtual ICollection<Publication> Publications { get; set; } = new List<Publication>();
}
19 changes: 4 additions & 15 deletions core/Data/Entities/Report.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.Schema;

using api.core.Data.Entities;

using api.core.Data.Enums;

Expand All @@ -11,24 +10,14 @@ namespace api.core.data.entities;

[Table("Report")]
[Index("PublicationId", Name = "IX_Report_PublicationId")]
public partial class Report
public partial class Report : BaseEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }

public string Reason { get; set; } = null!;

public ReportCategory Category { get; set; }

public Guid PublicationId { get; set; }

public DateTime CreatedAt { get; set; }

public DateTime UpdatedAt { get; set; }

public DateTime? DeletedAt { get; set; }

[ForeignKey("PublicationId")]
[InverseProperty("Reports")]
public virtual Publication Publication { get; set; } = null!;
Expand Down
20 changes: 4 additions & 16 deletions core/Data/Entities/Tag.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.Schema;

using api.core.Data.Entities;

using Microsoft.EntityFrameworkCore;

namespace api.core.data.entities;

[Table("Tag")]
public partial class Tag
public partial class Tag : BaseEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }

public string Name { get; set; } = null!;

public int PriorityValue { get; set; }

public DateTime CreatedAt { get; set; }

public DateTime UpdatedAt { get; set; }

public DateTime? DeletedAt { get; set; }

[ForeignKey("ParentTagsId")]
[InverseProperty("ParentTags")]
public virtual ICollection<Tag> ChildrenTags { get; set; } = new List<Tag>();
Expand Down
7 changes: 7 additions & 0 deletions core/Data/Entities/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace api.core.Data.Entities;

public class User : BaseEntity
{
public string Email { get; set; } = null!;

}
6 changes: 6 additions & 0 deletions core/Data/Exceptions/HttpException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ public abstract class HttpException : Exception

public HttpException()
{
StatusCode = 500;
ErrorCode = "INTERNAL_SERVER_ERROR";
}

public HttpException(string message)
: base(message)
{
StatusCode = 500;
ErrorCode = "INTERNAL_SERVER_ERROR";
}

public HttpException(string message, Exception inner)
: base(message, inner)
{
StatusCode = 500;
ErrorCode = "INTERNAL_SERVER_ERROR";
}
}
1 change: 0 additions & 1 deletion core/Data/Exceptions/NotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public NotFoundException()

/// <summary>Throws an <see cref="ArgumentNullException"/> if <paramref name="argument"/> is null.</summary>
/// <param name="argument">The reference type argument to validate as non-null.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
public static void ThrowIfNull(T? argument)
{
if (argument is null)
Expand Down
4 changes: 2 additions & 2 deletions core/Data/Requests/EventRequestDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public class EventRequestDTO

public virtual ICollection<Guid> Tags { get; set; } = new List<Guid>();

public string ImageAltText { get; set; }
public string ImageAltText { get; set; } = null!;
}


public class EventCreationRequestDTO : EventRequestDTO
{
public IFormFile Image { get; set; }
public IFormFile Image { get; set; } = null!;
}

public class EventUpdateRequestDTO : EventRequestDTO
Expand Down
4 changes: 2 additions & 2 deletions core/Data/Requests/ReportRequestDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace api.core.Data.Requests;

public class CreateReportRequestDTO
{
public string Reason { get; set; }
public required string Reason { get; set; }

public ReportCategory Category { get; set; }
public required ReportCategory Category { get; set; }
}
7 changes: 6 additions & 1 deletion core/Data/Requests/UserRequestDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class UserCreateDTO
{
public required string Email { get; set; }

public string? Organisation { get; set; } = null!;
public string? Organization { get; set; } = null!;

public string? ActivityArea { get; set; } = null!;
}
Expand All @@ -31,3 +31,8 @@ public class UserUpdateDTO : UserCreateDTO

public string? WebSiteLink { get; set; }
}

public class UserAvatarUpdateDTO
{
public required IFormFile avatarFile { get; set; }
}
12 changes: 6 additions & 6 deletions core/Data/Responses/ReportResponseDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ namespace api.core.Data.Responses;

public class ReportResponseDTO
{
public Guid Id { get; set; }
public required Guid Id { get; set; }

public string Reason { get; set; }
public required string Reason { get; set; }

public ReportCategory Category { get; set; }
public required ReportCategory Category { get; set; }

public EventResponseDTO Publication { get; set; }
public required EventResponseDTO Publication { get; set; }

public DateTime CreatedAt { get; set; }
public required DateTime CreatedAt { get; set; }

public DateTime UpdatedAt { get; set; }
public required DateTime UpdatedAt { get; set; }

public static ReportResponseDTO Map(Report report)
{
Expand Down
4 changes: 2 additions & 2 deletions core/Data/Responses/TagResponseDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class TagDetailsResponseDTO : TagResponseDTO
{
public int PriorityValue { get; set; }

public IEnumerable<TagDetailsResponseDTO> Children { get; set; }
public IEnumerable<TagDetailsResponseDTO> Children { get; set; } = new List<TagDetailsResponseDTO>();

public static TagDetailsResponseDTO Map(Tag oneTag)
public static new TagDetailsResponseDTO Map(Tag oneTag)
{
return new TagDetailsResponseDTO
{
Expand Down
4 changes: 3 additions & 1 deletion core/Data/Responses/UserResponseDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class UserResponseDTO

public string Email { get; set; } = null!;

public string AvatarUrl { get; set; } = null!;

public string Type { get; set; } = null!;

public string? Organisation { get; set; }
Expand Down Expand Up @@ -47,7 +49,7 @@ public static UserResponseDTO Map(Organizer organizer)
Id = organizer.Id,
Email = organizer.Email,
Type = "Organizer",
Organisation = organizer.Organisation,
Organisation = organizer.Organization,
ActivityArea = organizer.ActivityArea,
IsActive = organizer.IsActive,
ProfileDescription = organizer.ProfileDescription,
Expand Down
Loading

0 comments on commit ed2ceba

Please sign in to comment.