Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Commit

Permalink
Clean Up Code
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddmd committed Dec 26, 2022
1 parent 2f6c4cf commit 92540aa
Show file tree
Hide file tree
Showing 65 changed files with 299 additions and 374 deletions.
2 changes: 1 addition & 1 deletion Backend/Controllers/ActivityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public async Task<ActionResult<TripModelAdmin>> UpdateActivity(int id, ActivityU
{
await _activityRepository.UpdateActivity(activity, model);
await _tripRepository.RecalculateTripDistanceAndBudget(trip);
return Ok(_mapper.Map<Trip,TripModelAdmin>(trip));
return Ok(_mapper.Map<Trip, TripModelAdmin>(trip));
}
catch (CustomException exception)
{
Expand Down
14 changes: 8 additions & 6 deletions Backend/Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async Task<IActionResult> Register(UserRegisterModel model)
}
}
Ranking ranking = await rankingRepository.GetDefaultRanking();
if (ranking==null)
if (ranking == null)
{
ranking = await rankingRepository.CreateDefaultRanking();
}
Expand Down Expand Up @@ -113,7 +113,8 @@ public async Task<IActionResult> Register(UserRegisterModel model)
return BadRequest(new ErrorModel() { ErrorType = ErrorType.INVALID_COUNTRY_CODE, Message = "A country with this code doesn't exist" });
}
int NumberUsers = await userManager.Users.CountAsync();
User user = new() {
User user = new()
{
Email = model.Email,
UserName = model.UserName,
Name = model.Name,
Expand All @@ -125,9 +126,9 @@ public async Task<IActionResult> Register(UserRegisterModel model)
Locale = model.Locale,
CreationDate = DateTime.Now,
IsActive = true,
Facebook="",
Twitter="",
Instagram=""
Facebook = "",
Twitter = "",
Instagram = ""
};
IdentityResult result = await userManager.CreateAsync(user, model.Password);
if (!result.Succeeded)
Expand Down Expand Up @@ -211,7 +212,8 @@ public async Task<IActionResult> Login(UserLoginModel model)
new Claim(ClaimTypes.NameIdentifier, user.Id),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
};
foreach (string userRole in userRoles){
foreach (string userRole in userRoles)
{
authClaims.Add(new Claim(ClaimTypes.Role, userRole));
}
SymmetricSecurityKey authSigningKey = new(Encoding.UTF8.GetBytes(_configuration["JWT:Secret"]));
Expand Down
39 changes: 20 additions & 19 deletions Backend/Controllers/GroupsController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using AutoMapper;
using BackendAPI.Entities;
using BackendAPI.Repositories;
using AutoMapper;
using BackendAPI.Models.Group;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using BackendAPI.Entities.Enums;
using BackendAPI.Exceptions;
using BackendAPI.Models;
using BackendAPI.Models.Group;
using BackendAPI.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BackendAPI.Controllers
{
Expand Down Expand Up @@ -55,7 +55,7 @@ public async Task<ActionResult<IEnumerable<GroupModelSimple>>> GetAllGroups()
IEnumerable<Group> groups = await _repository.GetAll();
if (!is_admin)
{
groups = groups.Where(g => !(g.IsPrivate && !user.Groups.Any(ug=>ug.Group.Id == g.Id)));
groups = groups.Where(g => !(g.IsPrivate && !user.Groups.Any(ug => ug.Group.Id == g.Id)));
IEnumerable<GroupModelSimple> groups_ret = from g in groups select _mapper.Map<Group, GroupModelSimple>(g);
return Ok(groups_ret);
}
Expand Down Expand Up @@ -173,7 +173,7 @@ public async Task<ActionResult> SendInvite(GroupSendInviteModel model)
bool is_admin = user_roles.Contains(Enum.GetName(UserRole.ADMIN));
Group group = await _repository.GetById(model.GroupId);
User user_to_add = await _userManager.FindByIdAsync(model.UserId);
if (group == null|| user_to_add == null)
if (group == null || user_to_add == null)
{
return NotFound();
}
Expand Down Expand Up @@ -261,8 +261,8 @@ public async Task<ActionResult<GroupModelAdmin>> CreateGroup(GroupCreateModel gr
try
{
await _repository.Create(group);
await _repository.AddUser(group,user,null,true);
return Ok(_mapper.Map<Group,GroupModelAdmin>(group));
await _repository.AddUser(group, user, null, true);
return Ok(_mapper.Map<Group, GroupModelAdmin>(group));
}
catch (CustomException exception)
{
Expand Down Expand Up @@ -409,7 +409,7 @@ public async Task<ActionResult> JoinGroup(GroupJoinModel model)
try
{
//se for um admin, pode ignorar o requisito de convite caso o grupo seja privado
await _repository.AddUser(group, user,model.InviteId,is_admin);
await _repository.AddUser(group, user, model.InviteId, is_admin);
return Ok();
}
catch (CustomException exception)
Expand Down Expand Up @@ -485,7 +485,8 @@ public async Task<IActionResult> DeleteGroup(int id)
return NotFound();
}
UserGroupRole role = await _repository.GetUserRole(group, user);
if (!(is_admin || role == UserGroupRole.MANAGER)){
if (!(is_admin || role == UserGroupRole.MANAGER))
{
return Forbid();
}
try
Expand Down Expand Up @@ -616,13 +617,13 @@ public async Task<ActionResult> BanUser(GroupBanUserModel model)
IList<string> user_roles = await _userManager.GetRolesAsync(CurrentUser);
bool is_admin = user_roles.Contains(Enum.GetName(UserRole.ADMIN));
UserGroupRole role = await _repository.GetUserRole(group, CurrentUser);
if (!(role== UserGroupRole.MANAGER|| role == UserGroupRole.MODERATOR || is_admin))
if (!(role == UserGroupRole.MANAGER || role == UserGroupRole.MODERATOR || is_admin))
{
return Forbid();
}
try
{
await _repository.BanUser(group,user,model.BanReason,model.BanUntil,model.HidePosts);
await _repository.BanUser(group, user, model.BanReason, model.BanUntil, model.HidePosts);
return Ok();
}
catch (CustomException exception)
Expand Down
2 changes: 1 addition & 1 deletion Backend/Controllers/InfoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<ActionResult<RecommendationModel>> GetRecommendations()
{
User current_user = await _userManager.GetUserAsync(this.User);
Recommendation recommendation = await _informationRepository.GetRecommendations(current_user);
RecommendationModel model = _mapper.Map<Recommendation,RecommendationModel>(recommendation);
RecommendationModel model = _mapper.Map<Recommendation, RecommendationModel>(recommendation);
return Ok(model);
}
[Authorize(Roles = "ADMIN")]
Expand Down
20 changes: 10 additions & 10 deletions Backend/Controllers/PostsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public PostsController(IMapper mapper, ITripRepository tripRepository, IGroupRep
public async Task<ActionResult<IEnumerable<PostModel>>> GetAllPosts()
{
IEnumerable<Post> posts = await _postRepository.GetAll();
IEnumerable<PostModel> posts_ret = from post in posts select _mapper.Map<Post,PostModel>(post);
IEnumerable<PostModel> posts_ret = from post in posts select _mapper.Map<Post, PostModel>(post);
return Ok(posts_ret);
}
/// <summary>
Expand Down Expand Up @@ -99,8 +99,8 @@ public async Task<ActionResult<PostModel>> GetById(int Id)
return NotFound();
}
if (
(post.Trip.IsPrivate && !current_user.Trips.Any(cut=>cut.Trip.Id == post.Trip.Id)) ||
(post.Trip.Group.IsPrivate && !current_user.Groups.Any(cug=>cug.Group.Id == post.Trip.Group.Id))
(post.Trip.IsPrivate && !current_user.Trips.Any(cut => cut.Trip.Id == post.Trip.Id)) ||
(post.Trip.Group.IsPrivate && !current_user.Groups.Any(cug => cug.Group.Id == post.Trip.Group.Id))
)
{
return NotFound();
Expand Down Expand Up @@ -147,17 +147,17 @@ public async Task<ActionResult<PostModelTrip>> CreatePost(PostCreateModel model)
{
return NotFound();
}
if ((await _tripRepository.GetUserTrip(trip,user) == null || await _groupRepository.GetUserGroup(trip.Group,user) == null) && !is_admin)
if ((await _tripRepository.GetUserTrip(trip, user) == null || await _groupRepository.GetUserGroup(trip.Group, user) == null) && !is_admin)
{
return BadRequest(new ErrorModel() { ErrorType = ErrorType.POST_USER_NOT_IN_TRIP, Message = "Can't create a post since the user isn't in the trip"});
return BadRequest(new ErrorModel() { ErrorType = ErrorType.POST_USER_NOT_IN_TRIP, Message = "Can't create a post since the user isn't in the trip" });
}
Post post = _mapper.Map<PostCreateModel, Post>(model);
post.User = user;
post.Trip = trip;
try
{
await _postRepository.Create(post);
return Ok(_mapper.Map<Post,PostModel>(post));
return Ok(_mapper.Map<Post, PostModel>(post));
}
catch (CustomException exception)
{
Expand Down Expand Up @@ -206,14 +206,14 @@ public async Task<ActionResult> UpdatePost(int Id, PostUpdateModel model)
{
return NotFound();
}
if (!(role==UserGroupRole.MANAGER || role == UserGroupRole.MODERATOR || is_admin) && current_user != post.User)
if (!(role == UserGroupRole.MANAGER || role == UserGroupRole.MODERATOR || is_admin) && current_user != post.User)
{
return Forbid();
}
try
{
await _postRepository.Update(post, model);
return Ok(_mapper.Map<Post,PostModel>(post));
return Ok(_mapper.Map<Post, PostModel>(post));
}
catch (CustomException exception)
{
Expand Down Expand Up @@ -318,8 +318,8 @@ public async Task<ActionResult<PostModel>> AddAttachment([FromForm] int postId,
}
try
{
await _postRepository.AddAttachment(post,file);
return Ok(_mapper.Map<Post,PostModel>(post));
await _postRepository.AddAttachment(post, file);
return Ok(_mapper.Map<Post, PostModel>(post));
}
catch (CustomException exception)
{
Expand Down
6 changes: 3 additions & 3 deletions Backend/Controllers/RankingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task<ActionResult<RankingModelAdmin>> GetById(int Id)
{
return NotFound();
}
return _mapper.Map<Ranking,RankingModelAdmin>(ranking);
return _mapper.Map<Ranking, RankingModelAdmin>(ranking);
}
/// <summary>
/// Atualizar os detalhes de um ranking no sistema
Expand Down Expand Up @@ -87,7 +87,7 @@ public async Task<ActionResult<RankingModelAdmin>> UpdateRanking(int Id, Ranking
{
return BadRequest(new ErrorModel() { ErrorType = exception.ErrorType, Message = exception.Message });
}
catch(Exception ex)
catch (Exception ex)
{
return BadRequest(new ErrorModel() { ErrorType = ErrorType.OTHER, Message = ex.Message });
}
Expand All @@ -102,7 +102,7 @@ public async Task<ActionResult<RankingModelAdmin>> UpdateRanking(int Id, Ranking
[HttpPost]
public async Task<ActionResult<RankingModelAdmin>> CreateRanking(RankingCreateModel model)
{
Ranking r = _mapper.Map<RankingCreateModel,Ranking>(model);
Ranking r = _mapper.Map<RankingCreateModel, Ranking>(model);
try
{
await _repository.Create(r);
Expand Down
32 changes: 16 additions & 16 deletions Backend/Controllers/TripsController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AutoMapper;
using BackendAPI.Entities;
using AutoMapper;
using BackendAPI.Repositories;
using BackendAPI.Models.Trip;
using Microsoft.AspNetCore.Identity;
using BackendAPI.Entities.Enums;
using Microsoft.AspNetCore.Authorization;
using BackendAPI.Exceptions;
using BackendAPI.Models;
using BackendAPI.Models.Trip;
using BackendAPI.Repositories;
using GoogleApi.Entities.Maps.Directions.Response;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using BackendAPI.Models;
using BackendAPI.Exceptions;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BackendAPI.Controllers
{
Expand Down Expand Up @@ -69,7 +69,7 @@ public async Task<ActionResult<TripModel>> CreateTrip(TripCreateModel tripCreate
try
{
await _repository.Create(trip, group);
await _repository.AddUser(trip,current_user,null,true);
await _repository.AddUser(trip, current_user, null, true);
return Ok(_mapper.Map<Trip, TripModelAdmin>(trip)); //redireciona depois baseado no ID
}
catch (CustomException exception)
Expand Down Expand Up @@ -106,7 +106,7 @@ public async Task<ActionResult<IEnumerable<TripModel>>> GetAllTrips()
bool is_admin = user_roles.Contains(Enum.GetName(UserRole.ADMIN));
if (!is_admin)
{
trips = trips.Where(t =>
trips = trips.Where(t =>
!(t.Group.IsPrivate && !user.Groups.Any(ug => ug.Group.Id == t.Group.Id)) &&
!(t.IsPrivate && !user.Trips.Any(ut => ut.Trip.Id == t.Id))).ToList();
IEnumerable<TripModelSimple> tripsList = from trip in trips select _mapper.Map<Trip, TripModelSimple>(trip);
Expand Down Expand Up @@ -207,7 +207,7 @@ public async Task<ActionResult> JoinTrip(TripJoinModel model)
}
try
{
await _repository.AddUser(trip, user,model.InviteId, (is_admin|| IsManager));
await _repository.AddUser(trip, user, model.InviteId, (is_admin || IsManager));
return Ok();
}
catch (CustomException exception)
Expand Down Expand Up @@ -283,7 +283,7 @@ public async Task<ActionResult<TripModel>> RemoveUser(TripRemoveUserModel model)
}
try
{
await _repository.RemoveUser(trip,user_to_remove);
await _repository.RemoveUser(trip, user_to_remove);
return Ok();
}
catch (CustomException exception)
Expand Down
32 changes: 16 additions & 16 deletions Backend/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using AutoMapper;
using BackendAPI.Entities;
using BackendAPI.Entities.Enums;
using BackendAPI.Exceptions;
using BackendAPI.Models;
using BackendAPI.Models.User;
using AutoMapper;
using BackendAPI.Repositories;
using Microsoft.AspNetCore.Authorization;
using BackendAPI.Entities.Enums;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using BackendAPI.Exceptions;
using BackendAPI.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BackendAPI.Controllers
{
Expand Down Expand Up @@ -45,7 +45,7 @@ public UsersController(IMapper mapper, IUserRepository repository, UserManager<U
public async Task<ActionResult<IEnumerable<UserModel>>> GetAllUsers()
{
IEnumerable<User> users = await _repository.GetAll();
IEnumerable<UserModelSelf> users_ret = from user in users select _mapper.Map<User,UserModelSelf>(user);
IEnumerable<UserModelSelf> users_ret = from user in users select _mapper.Map<User, UserModelSelf>(user);
return Ok(users_ret);
}
/// <summary>
Expand Down Expand Up @@ -97,11 +97,11 @@ public async Task<ActionResult<UserModel>> GetUserById(String id)
!(ug.Group.IsPrivate &&
!current_user.Groups.Any(gg => gg.Group.Id == ug.Group.Id))).ToList();
//Esconder Trips e Posts de Trips que estejam escondidas e o utilizador não pertença à mesma, ou que pertençam a grupos privados que o utilizador actual não esteja presente
user.Trips = user.Trips.Where(ut =>
!(ut.Trip.IsPrivate && !current_user.Trips.Any(cut=>cut.Trip.Id == ut.Trip.Id)) &&
user.Trips = user.Trips.Where(ut =>
!(ut.Trip.IsPrivate && !current_user.Trips.Any(cut => cut.Trip.Id == ut.Trip.Id)) &&
!(ut.Trip.Group.IsPrivate && !current_user.Groups.Any(gg => gg.Group.Id == ut.Trip.Group.Id))).ToList();
user.Posts = user.Posts.Where(up =>
!(up.Trip.IsPrivate && !current_user.Trips.Any(cut => cut.Trip.Id == up.Trip.Id)) &&
user.Posts = user.Posts.Where(up =>
!(up.Trip.IsPrivate && !current_user.Trips.Any(cut => cut.Trip.Id == up.Trip.Id)) &&
!(up.Trip.Group.IsPrivate && !current_user.Groups.Any(gg => gg.Group.Id == up.Trip.Group.Id)) &&
!up.IsHidden).ToList();
user.Following = user.Following.Where(f => f.IsActive).ToList();
Expand Down Expand Up @@ -338,7 +338,7 @@ public async Task<ActionResult> DeleteOwnPicture()
/// <returns></returns>
/// <response code="200">Remoção do Utilizador do Sistema bem-sucedida</response>
/// <response code="404">Não existe um utilizador no sistema com este ID</response>
[Authorize(Roles ="ADMIN")]
[Authorize(Roles = "ADMIN")]
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteUser(String Id)
{
Expand Down
4 changes: 2 additions & 2 deletions Backend/Entities/Activity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using BackendAPI.Entities.Enums;
using BackendAPI.Entities.Enums;
using System;
namespace BackendAPI.Entities
{
public class Activity
Expand Down
7 changes: 1 addition & 6 deletions Backend/Entities/Enums/ActivityType.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BackendAPI.Entities.Enums
namespace BackendAPI.Entities.Enums
{
public enum ActivityType
{
Expand Down
Loading

0 comments on commit 92540aa

Please sign in to comment.