92 lines
3.5 KiB
C#
92 lines
3.5 KiB
C#
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SessionCompanion.Extensions.EitherType;
|
|
using SessionCompanion.Services.Interfaces;
|
|
using SessionCompanion.ViewModels.ApiResponses;
|
|
using SessionCompanion.ViewModels.CharacterViewModels;
|
|
|
|
namespace SessionCompanion.Controllers
|
|
{
|
|
using SessionCompanion.Hubs;
|
|
using SessionCompanion.ViewModels.UniversalModels;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
|
|
[Route("api/character")]
|
|
[ApiController]
|
|
public class CharacterController : Controller
|
|
{
|
|
private readonly ICharacterService _service;
|
|
private readonly SessionHubData _sessionHubData;
|
|
|
|
public CharacterController(ICharacterService service)
|
|
{
|
|
this._service = service;
|
|
this._sessionHubData = new SessionHubData();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Metoda zwraca postać ze wskazanym identyfikatorem
|
|
/// </summary>
|
|
/// <param name="characterId">Identyfikator postaci</param>
|
|
/// <returns>ViewModel Postaci/ErrorResponse</returns>
|
|
[HttpGet("{characterId}")]
|
|
public async Task<Either<ErrorResponse, CharacterViewModel>> Get(int characterId)
|
|
{
|
|
|
|
CharacterViewModel characterViewModel = await _service.Get(characterId);
|
|
if (characterViewModel is null)
|
|
return new ErrorResponse() {
|
|
StatusCode = 204,
|
|
Message = "No character with given characterId"
|
|
};
|
|
return characterViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Metoda zwraca listę postaci przypisanych do danego użytkownika
|
|
/// </summary>
|
|
/// <param name="userId"> Identyfikator użytkownika </param>
|
|
/// <returns> Lista postaci lub wiadomość błędu </returns>
|
|
[HttpGet("userCharactersList")]
|
|
public async Task<Either<List<CharacterForLoginViewModel>, ErrorResponse>> GetCharacterListForUser([Required] int userId)
|
|
{
|
|
var charactersList = await this._service.GetUserLoginCharacters(userId);
|
|
|
|
if (charactersList.Any())
|
|
{
|
|
return charactersList.ToList();
|
|
}
|
|
|
|
return new ErrorResponse()
|
|
{
|
|
StatusCode = 204,
|
|
Message = "No characters with given user id"
|
|
};
|
|
}
|
|
/// <summary>
|
|
/// Metoda zwraca listę postaci przypisanych do danego użytkownika
|
|
/// </summary>
|
|
/// <returns> Lista zalogowanych postaci lub wiadomość błędu </returns>
|
|
[HttpGet("loggedCharacters")]
|
|
public async Task<Either<List<CharacterBasicStatsViewModel>, ErrorResponse>> GetLoggedUsersCharacters()
|
|
{
|
|
var connectedCharacters = _sessionHubData.ConnectedCharacters_Prop;
|
|
var characters = await _service.GetBasicCharactersData(connectedCharacters.Values.ToList());
|
|
|
|
return characters.ToList();
|
|
}
|
|
/// <summary>
|
|
/// Metoda zwraca wszystkie statystyki dla danej postaci
|
|
/// </summary>
|
|
/// <param name="characterId"> Id postaci </param>
|
|
/// <returns> Listę wszystkich statystyk </returns>
|
|
[HttpGet("characterStats")]
|
|
public async Task<Either<List<UniversalStatisticViewModel>, ErrorResponse>> GetCharacterEveryStat([Required] int characterId)
|
|
{
|
|
var statistics = await _service.GetCharacterStatistics(characterId);
|
|
return statistics;
|
|
}
|
|
}
|
|
} |