Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Commit

Permalink
Release v1.1 (#23)
Browse files Browse the repository at this point in the history
* Date render fix

* LOT of features and bug fixes

-Added profile picture updating
-Added a video player.
-Added a session expired message.
-Fixed users being able to register with the same email.
-Fixed dates not being rendered correctly.

-Fixed replies order.
-Fixed #22
-Fixed #21
-Done #20
-Done #8
-Done #7
-Done #3
-Done #9
-Done #12
-Done #10

and probably way more that I forgot.
  • Loading branch information
laurentksh authored Feb 9, 2021
1 parent 95772b5 commit 4fb656d
Show file tree
Hide file tree
Showing 40 changed files with 696 additions and 16,282 deletions.
14 changes: 8 additions & 6 deletions src/back/ICT-151/ICT-151/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public class UserController : ControllerBase
{
private readonly ILogger<UserController> Logger;
private readonly IUserService UserService;
private readonly IMediaService MediaService;
private readonly IExceptionHandlerService ExceptionHandlerService;

public UserController(ILogger<UserController> logger, IUserService userService, IExceptionHandlerService exceptionHandlerService)
public UserController(ILogger<UserController> logger, IUserService userService, IMediaService mediaService, IExceptionHandlerService exceptionHandlerService)
{
Logger = logger;
UserService = userService;
MediaService = mediaService;
ExceptionHandlerService = exceptionHandlerService;
}

Expand Down Expand Up @@ -251,21 +253,21 @@ public async Task<IActionResult> UnBlock(Guid userId)
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<IActionResult> SetProfilePicture([FromQuery] Guid mediaId)
public async Task<IActionResult> SetProfilePicture([FromBody] ProfilePictureUpdate update)
{
try {
var user = await HttpContext.GetUser();
await UserService.SetProfilePicture(user.Id, mediaId);
var result = await UserService.SetProfilePicture(user.Id, update.MediaId);

return Ok();
return Ok(result);
} catch (Exception ex) {
Logger.LogWarning(ex, "An error occured: " + ex.Message ?? "undefined");
return ExceptionHandlerService.Handle(ex, Request);
}
}

[HttpDelete("profilepicture")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(MediaViewModel))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> RemoveProfilePicture()
Expand All @@ -274,7 +276,7 @@ public async Task<IActionResult> RemoveProfilePicture()
var user = await HttpContext.GetUser();
await UserService.RemoveProfilePicture(user.Id);

return Ok();
return Ok(await MediaService.GetDefaultProfilePicture());
} catch (Exception ex) {
Logger.LogWarning(ex, "An error occured: " + ex.Message ?? "undefined");
return ExceptionHandlerService.Handle(ex, Request);
Expand Down
8 changes: 8 additions & 0 deletions src/back/ICT-151/ICT-151/Models/Dto/UserDtos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,19 @@ public class UpdateUserDto
public DateTime? BirthDay { get; set; }
}

public class ProfilePictureUpdate
{
public Guid MediaId { get; set; }
}

public class UserSummaryViewModel
{
public Guid Id { get; set; }

public string Username { get; set; }

public string Email { get; set; }

public string ProfilePictureId { get; set; }

public string Biography { get; set; }
Expand All @@ -85,6 +92,7 @@ public class UserSummaryViewModel
{
Id = user.Id,
Username = user.Username,
Email = userId.HasValue && user.Id == userId.Value ? user.Email : null,
ProfilePictureId = user.ProfilePictureMediaId == null ? "default_pp" : user.ProfilePictureMediaId.ToString(),
Biography = user.Biography,
Birthday = user.Birthday,
Expand Down
14 changes: 12 additions & 2 deletions src/back/ICT-151/ICT-151/Repositories/PublicationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task<PublicationViewModel> GetPublication(Guid id, Guid? requestUse
public async Task<IEnumerable<PublicationViewModel>> GetReplies(Guid id, Guid? requestUserId)
{
return DbContext.Publications
.OrderBy(x => x.CreationDate)
.OrderByDescending(x => x.CreationDate)
.Include(x => x.User)
.Include(x => x.Replies)
.Include(x => x.Reposts)
Expand All @@ -75,7 +75,7 @@ public async Task<IEnumerable<PublicationViewModel>> GetReplies(Guid id, Guid? r
public async Task<IEnumerable<RepostViewModel>> GetReposts(Guid publicationId)
{
return DbContext.Reposts
.OrderBy(x => x.CreationDate)
.OrderByDescending(x => x.CreationDate)
.Include(x => x.User)
.Where(x => x.PublicationId == publicationId)
.Select(x => RepostViewModel.FromRepost(x));
Expand All @@ -84,6 +84,7 @@ public async Task<IEnumerable<RepostViewModel>> GetReposts(Guid publicationId)
public async Task<IEnumerable<LikeViewModel>> GetLikes(Guid publicationId)
{
return DbContext.Likes
.OrderByDescending(x => x.CreationDate)
.Include(x => x.User)
.Where(x => x.PublicationId == publicationId)
.Select(x => LikeViewModel.FromLike(x));
Expand All @@ -108,10 +109,19 @@ public async Task<PublicationViewModel> CreateNew(Guid userId, PublicationCreate

public async Task Remove(Guid userId, Guid publicationId)
{
var replies = DbContext.Publications.Where(x => x.ReplyPublicationId == publicationId);
DbContext.Publications.RemoveRange(replies);
DbContext.Publications.Remove(await DbContext.Publications.SingleAsync(x => x.Id == publicationId && x.UserId == userId));
await DbContext.SaveChangesAsync();
}

public async Task RemoveReplies(Guid publicationId)
{
var replies = DbContext.Publications.Where(x => x.ReplyPublicationId == publicationId);
DbContext.Publications.RemoveRange(replies);
await DbContext.SaveChangesAsync();
}

public async Task Repost(Guid userId, Guid publicationId)
{
await DbContext.Reposts.AddAsync(new Repost
Expand Down
7 changes: 7 additions & 0 deletions src/back/ICT-151/ICT-151/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public interface IUserRepository
Task<bool> FollowExists(Guid followerId, Guid followedId);

Task<bool> BlockExists(Guid blockerId, Guid blockedId);

Task<bool> EmailExists(string email);
}

public class UserRepository : IUserRepository
Expand Down Expand Up @@ -373,5 +375,10 @@ public async Task<bool> BlockExists(Guid blockerId, Guid blockedId)
{
return await DbContext.Blocks.AnyAsync(x => x.BlockerId == blockerId && x.BlockTargetId == blockedId);
}

public async Task<bool> EmailExists(string email)
{
return await DbContext.Users.AnyAsync(x => x.Email == email);
}
}
}
29 changes: 25 additions & 4 deletions src/back/ICT-151/ICT-151/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public interface IUserService
/// <param name="userId">User Id</param>
/// <param name="mediaId">Media Id</param>
/// <returns></returns>
Task SetProfilePicture(Guid userId, Guid mediaId);
Task<MediaViewModel> SetProfilePicture(Guid userId, Guid mediaId);

/// <summary>Remove the profile picture of a specified user.</summary>
/// <param name="userId">User Id</param>
Expand All @@ -175,6 +175,13 @@ public interface IUserService
/// <param name="id">Username to check</param>
/// <returns>True if the user exists, otherwise false</returns>
Task<bool> Exists(string username);

/// <summary>
/// Check if the email is already used
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
Task<bool> EmailExists(string email);
}

public class UserService : IUserService
Expand Down Expand Up @@ -294,6 +301,8 @@ public async Task<CreatedUserViewModel> CreateNew(CreateUserDto dto, IPAddress r
throw new ArgumentOutOfRangeException(nameof(remoteHost), "Invalid IP Address");
if (await Exists(dto.Username))
throw new ArgumentException("Username already taken.", nameof(dto));
if (await EmailExists(dto.Email))
throw new ArgumentException("Email already taken.", nameof(dto));

dto.Password = Utilities.StringUtilities.ComputeHash(dto.Password, System.Security.Cryptography.HashAlgorithmName.SHA512);

Expand All @@ -310,6 +319,10 @@ public async Task<UserSummaryViewModel> Update(Guid userId, UpdateUserDto dto, I
if (!await Exists(userId))
throw new UserNotFoundException("User does not exist.");

var user = await UserRepository.GetBaseUser(userId);
if (await EmailExists(dto.Email) && user.Email != dto.Email)
throw new ArgumentException("Email already taken.", nameof(dto));

if (dto.NewPassword != null) {
dto.Password = Utilities.StringUtilities.ComputeHash(dto.Password, System.Security.Cryptography.HashAlgorithmName.SHA512);
dto.NewPassword = Utilities.StringUtilities.ComputeHash(dto.NewPassword, System.Security.Cryptography.HashAlgorithmName.SHA512);
Expand Down Expand Up @@ -403,15 +416,18 @@ public async Task<List<PrivateMessageViewModel>> GetPrivateMessages(Guid userId,
return (await UserRepository.GetPrivateMessages(userId, recipientId)).ToList();
}

public async Task SetProfilePicture(Guid userId, Guid mediaId)
public async Task<MediaViewModel> SetProfilePicture(Guid userId, Guid mediaId)
{
if (!await Exists(userId) || !await MediaService.Exists(mediaId))
if (!await Exists(userId))
throw new UserNotFoundException("User does not exist.");
if (!await MediaService.Exists(mediaId))
throw new DataNotFoundException("Media does not exist.");

if (!await MediaService.HasAccess(userId, mediaId))
throw new ForbiddenException($"User {userId} does not have access to resource {mediaId}");

await UserRepository.SetProfilePicture(userId, mediaId);
return await MediaService.GetMedia(userId, mediaId);
}

public async Task RemoveProfilePicture(Guid userId)
Expand All @@ -431,5 +447,10 @@ public async Task<bool> Exists(string username)
{
return await UserRepository.Exists(username);
}

public async Task<bool> EmailExists(string email)
{
return await UserRepository.EmailExists(email);
}
}
}
1 change: 1 addition & 0 deletions src/front/ICT120/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"./node_modules/@videogular/ngx-videogular/fonts/videogular.css",
"src/styles.css"
],
"scripts": []
Expand Down
Loading

0 comments on commit 4fb656d

Please sign in to comment.