Skip to content

Commit

Permalink
Create entities crud operations (#8)
Browse files Browse the repository at this point in the history
* exclude services in separate project

* Revert "exclude services in separate project"

This reverts commit 049097a.

* Add Player CRUD operations, add automapper nuget to project

* add corrected service and repository registration

* player mapper registration

* add json ignoring cycles service registration

* add Game CRUD operations. corrected PLayer Service

* Corrected Create/Update Game oprtaions. Deleted sport type table and exchanged for enum type. Updated game mapping profile

* renamed class namespaces, correction of CRUD operations, changed Game entity class

* created unit tests for player/game repositories and controllers

* corrected names in game tests

* corrected  controllers and services

* changed names of particular unit tests

* structure fixes, code mistakes fixes

* amended status code in controllers

* code fixes in player and Game Controllers and tests

* replaced unneceaseary property duplication in Response.cs

* fixed incorrect response in player and  game controllers

* Added ServiceResponse class, refactored Game Service and GameController. Updated resources messages

* cahnged logic in player service and controller

* few minor changes to controller/models. Refactored Controllers unit tests

* deleted folder with custom exceptions. Rewrited unit tests for Game and Player service

* fixed empty lines

* fixed empty line in playerServiceTests

* fixed empty lines part 2
  • Loading branch information
EmilBulka authored Jan 31, 2024
1 parent 320c6e2 commit e1bcdea
Show file tree
Hide file tree
Showing 62 changed files with 2,757 additions and 1,069 deletions.
2 changes: 2 additions & 0 deletions Actively/Actively/ActivelyApp/ActivelyApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.18" Condition="'$(Configuration)' == 'Debug'" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" Condition="'$(Configuration)' == 'Debug'" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.14" />
<PackageReference Include="NETCore.MailKit" Version="2.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.18" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using Microsoft.AspNetCore.Authorization;
using ActivelyApp.Models.Authentication.Authentication;
using ActivelyApp.Models.AuthenticationDto.Email;
using ActivelyApp.Models.AuthenticationDto.Login;
using ActivelyApp.Models.AuthenticationDto.PasswordReset;
using ActivelyApp.Models.AuthenticationDto.Registration;
using ActivelyApp.Models.Common;
using ActivelyApp.Services.UserServices.EmailService;
using ActivelyDomain.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using ActivelyApp.Models.Authentication.Email;
using ActivelyApp.Models.Authentication.Login;
using ActivelyApp.Models.Authentication.Password;
using ActivelyApp.Models.Authentication.Registration;
using ActivelyApp.Services.UserServices.EmailService;
using Resources;
using System.ComponentModel.DataAnnotations;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using ActivelyApp.Models.Common;
using ActivelyDomain.Entities;
using System.Security.Cryptography;
using ActivelyApp.Models.Authentication.Authentication;
using System.Text;

namespace ActivelyApp.Controllers.Authentication
{
Expand Down
128 changes: 128 additions & 0 deletions Actively/Actively/ActivelyApp/Controllers/GameController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using ActivelyApp.Models.Common;
using ActivelyApp.Models.EntityDto;
using ActivelyApp.Services.EntityService;
using ActivelyDomain.Entities;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Resources;

namespace ActivelyApp.Controllers
{
[ApiController]
[Authorize]
[Route("[controller]/[action]")]
public class GameController : Controller
{
private readonly IGameService _gameService;
private readonly IMapper _mapper;

public GameController(IGameService gameService, IMapper mapper)
{
_gameService = gameService;
_mapper = mapper;
}

[HttpGet]
public async Task<IActionResult> GetAllGames()
{
var serviceResult = await _gameService.GetAllGames();
if (serviceResult.IsSuccess == true)
{
var games = _mapper.Map<IEnumerable<GameDto>>(serviceResult.Data);
return StatusCode(StatusCodes.Status200OK, new Response
{ Type = ResponseType.Success, Status = Common.Success, Content = games, Message = serviceResult.Message, IsSuccess = true });
}
else
{
return StatusCode(StatusCodes.Status400BadRequest, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
}

[HttpGet]
public async Task<IActionResult> GetGameById(int id)
{
var serviceResult = await _gameService.GetGameById(id);

if (serviceResult.IsSuccess == true)
{
var game = _mapper.Map<GameDto>(serviceResult.Data);
return StatusCode(StatusCodes.Status200OK, new Response
{ Type = ResponseType.Success, Status = Common.Success, Content = game, Message = serviceResult.Message, IsSuccess = true });
}
else
{
if (serviceResult.Message == Common.GameNotExists)
{
return StatusCode(StatusCodes.Status404NotFound, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
return StatusCode(StatusCodes.Status400BadRequest, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
}

[HttpPost]
public async Task<IActionResult> CreateGame([FromBody] CreateGameInfoDto newGame)
{
var entityNewGame = _mapper.Map<Game>(newGame);
var serviceResult = await _gameService.CreateGame(entityNewGame);
if (serviceResult.IsSuccess == true)
{
return StatusCode(StatusCodes.Status201Created, new Response
{ Type = ResponseType.Success, Status = Common.Success, Content = newGame, Message = serviceResult.Message, IsSuccess = true });
}
else
{
return StatusCode(StatusCodes.Status400BadRequest, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
}

[HttpPatch]
public async Task<IActionResult> UpdateGame([FromBody] UpdateGameInfoDto updateGameInfo, int id)
{
var entityUpdateGame = _mapper.Map<Game>(updateGameInfo);
var serviceResult = await _gameService.UpdateGame(entityUpdateGame, id);
if (serviceResult.IsSuccess == true)
{
return StatusCode(StatusCodes.Status200OK, new Response
{ Type = ResponseType.Success, Status = Common.Success, Content = updateGameInfo, Message = serviceResult.Message, IsSuccess = true });
}
else
{
if (serviceResult.Message == Common.GameNotExists)
{
return StatusCode(StatusCodes.Status404NotFound, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}

return StatusCode(StatusCodes.Status400BadRequest, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
}

[HttpDelete]
public async Task<IActionResult> DeleteGame(int id)
{
var serviceResult = await _gameService.DeleteGame(id);
if (serviceResult.IsSuccess == true)
{
return StatusCode(StatusCodes.Status200OK, new Response
{ Type = ResponseType.Success, Status = Common.Success, Message = serviceResult.Message, IsSuccess = true });
}
else
{
if (serviceResult.Message == Common.GameNotExists)
{
return StatusCode(StatusCodes.Status404NotFound, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}

return StatusCode(StatusCodes.Status400BadRequest, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
}
}
}
5 changes: 2 additions & 3 deletions Actively/Actively/ActivelyApp/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using ActivelyApp.Models;
using ActivelyApp.Models.Common;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using ActivelyApp.Models.Common;

namespace ActivelyApp.Controllers
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.Diagnostics;
using System.Globalization;
using System.Net;
using ActivelyApp.Models.Common;
using Microsoft.Extensions.Options;
using Resources;
using ActivelyApp.Models.Common;

namespace ActivelyApp.Controllers
{
Expand Down
128 changes: 128 additions & 0 deletions Actively/Actively/ActivelyApp/Controllers/PlayerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using ActivelyApp.Models.Common;
using ActivelyApp.Models.EntityDto;
using ActivelyApp.Services.EntityService;
using ActivelyDomain.Entities;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Resources;

namespace ActivelyApp.Controllers
{
[ApiController]
[Authorize]
[Route("[controller]/[action]")]
public class PlayerController : Controller
{
private readonly IPlayerService _playerService;
private readonly IMapper _mapper;

public PlayerController(IPlayerService playerService, IMapper mapper)
{
_playerService = playerService;
_mapper = mapper;
}

[HttpGet]
public async Task<IActionResult> GetAllPlayers()
{
var serviceResult = await _playerService.GetAllPlayers();
if (serviceResult.IsSuccess == true)
{
var players = _mapper.Map<IEnumerable<PlayerDto>>(serviceResult.Data);
return StatusCode(StatusCodes.Status200OK, new Response
{ Type = ResponseType.Success, Status = Common.Success, Content = players, Message = serviceResult.Message, IsSuccess = true });
}
else
{
return StatusCode(StatusCodes.Status400BadRequest, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
}

[HttpGet]
public async Task<IActionResult> GetPlayerById(int id)
{
var serviceResult = await _playerService.GetPlayerById(id);

if (serviceResult.IsSuccess == true)
{
var player = _mapper.Map<PlayerDto>(serviceResult.Data);
return StatusCode(StatusCodes.Status200OK, new Response
{ Type = ResponseType.Success, Status = Common.Success, Content = player, Message = serviceResult.Message, IsSuccess = true });
}
else
{
if (serviceResult.Message == Common.PlayerNotExists)
{
return StatusCode(StatusCodes.Status404NotFound, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
return StatusCode(StatusCodes.Status400BadRequest, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
}

[HttpPost]
public async Task<IActionResult> CreatePlayer([FromBody] CreatePlayerInfoDto newPlayer)
{
var entityNewPlayer = _mapper.Map<Player>(newPlayer);
var serviceResult = await _playerService.CreatePlayer(entityNewPlayer);
if (serviceResult.IsSuccess == true)
{
return StatusCode(StatusCodes.Status201Created, new Response
{ Type = ResponseType.Success, Status = Common.Success, Content = newPlayer, Message = serviceResult.Message, IsSuccess = true });
}
else
{
return StatusCode(StatusCodes.Status400BadRequest, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
}

[HttpPatch]
public async Task<IActionResult> UpdatePlayer([FromBody] UpdatePlayerInfoDto updatePlayerInfo, int id)
{
var entityUpdatePlayer = _mapper.Map<Player>(updatePlayerInfo);
var serviceResult = await _playerService.UpdatePlayer(entityUpdatePlayer, id);
if (serviceResult.IsSuccess == true)
{
return StatusCode(StatusCodes.Status200OK, new Response
{ Type = ResponseType.Success, Status = Common.Success, Content = updatePlayerInfo, Message = serviceResult.Message, IsSuccess = true });
}
else
{
if (serviceResult.Message == Common.PlayerNotExists)
{
return StatusCode(StatusCodes.Status404NotFound, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}

return StatusCode(StatusCodes.Status400BadRequest, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
}

[HttpDelete]
public async Task<IActionResult> DeletePlayer(int id)
{
var serviceResult = await _playerService.DeletePlayer(id);
if (serviceResult.IsSuccess == true)
{
return StatusCode(StatusCodes.Status200OK, new Response
{ Type = ResponseType.Success, Status = Common.Success, Message = serviceResult.Message, IsSuccess = true });
}
else
{
if (serviceResult.Message == Common.PlayerNotExists)
{
return StatusCode(StatusCodes.Status404NotFound, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}

return StatusCode(StatusCodes.Status400BadRequest, new Response
{ Type = ResponseType.Error, Status = Common.Error, Message = serviceResult.Message, IsSuccess = false });
}
}
}
}
17 changes: 17 additions & 0 deletions Actively/Actively/ActivelyApp/Mappings/GameMappingProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ActivelyApp.Models.EntityDto;
using ActivelyDomain.Entities;
using AutoMapper;

namespace ActivelyApp.Mappings
{
public class GameMappingProfile : Profile
{
public GameMappingProfile()
{
CreateMap<CreateGameInfoDto, Game>();
CreateMap<UpdateGameInfoDto, Game>();
CreateMap<Game, GameDto>();
CreateMap<GameDto, Game>();
}
}
}
16 changes: 16 additions & 0 deletions Actively/Actively/ActivelyApp/Mappings/PLayerMappingProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ActivelyApp.Models.EntityDto;
using ActivelyDomain.Entities;
using AutoMapper;

namespace ActivelyApp.Mappings
{
public class PlayerMappingProfile : Profile
{
public PlayerMappingProfile()
{
CreateMap<CreatePlayerInfoDto, Player>();
CreateMap<UpdatePlayerInfoDto, Player>();
CreateMap<Player, PlayerDto>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace ActivelyApp.Models.Authentication.Email
namespace ActivelyApp.Models.AuthenticationDto.Email
{
public class EmailConfiguration
{
public string From { get; set; } = null!;
public string SmtpServer { get; set; } = null!;
public int Port{ get; set; }
public int Port { get; set; }
public string UserName { get; set; } = null!;
public string Password { get; set; } = null!;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using MimeKit;

namespace ActivelyApp.Models.Authentication.Email
namespace ActivelyApp.Models.AuthenticationDto.Email
{
public class EmailMessage
{
public List<MailboxAddress>? To { get; set; }
public string? Subject { get; set; }
public string? Content { get; set; }

public EmailMessage(IEnumerable<string>to, string subject, string content)
public EmailMessage(IEnumerable<string> to, string subject, string content)
{
To = new List<MailboxAddress>();
To.AddRange(to.Select(x=> new MailboxAddress("Actively", x)));
To.AddRange(to.Select(x => new MailboxAddress("Actively", x)));
Subject = subject;
Content = content;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;

namespace ActivelyApp.Models.Authentication.Login
namespace ActivelyApp.Models.AuthenticationDto.Login
{
public class LoginModel
{
Expand Down
Loading

0 comments on commit e1bcdea

Please sign in to comment.