Knocks/BackEnd/Knoks.Api/Controllers/AccountController.cs

84 lines
3.2 KiB
C#

using Knoks.Api.Controllers.Base;
using Knoks.Core.Data.Interfaces;
using Knoks.Core.Entities;
using Knoks.Core.Entities.Args;
using Knoks.Core.Logic.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using Knoks.Framework.Document;
using System.IO;
using Knoks.Api.Entities;
namespace Knoks.Api.Controllers
{
[Route("v1/[controller]")]
public class AccountController : UserController
{
private readonly ILogger<AccountController> _logger;
private readonly IAccountDao _accountDao;
private readonly IGeneralManager _generalManager;
public AccountController(ILogger<AccountController> logger, IAccountDao accountDao, IGeneralManager generalManager)
{
_logger = logger;
_accountDao = accountDao;
_generalManager = generalManager;
}
[HttpGet("UserAccounts")]
public async Task<UserAccounts> GetUserAccounts()
{
var result = await _accountDao.GetUserAccounts(UserId);
if (result?.User?.HasAvatar == true)
{
result.User.AvatarUrl = _generalManager.GetAvatarUrl(result.User.UserId);
}
return result;
}
[HttpGet("GetUser")]
public async Task<UserAccounts> GetUserAccounts([FromQuery]long UserId)
{
var result = await _accountDao.GetUserAccounts(UserId);
if (result?.User?.HasAvatar == true)
{
result.User.AvatarUrl = _generalManager.GetAvatarUrl(result.User.UserId);
}
return result;
}
[HttpGet("favorites/get")]
public async Task<ApiArray<int>> GetFavorites()
{
return new ApiArray<int>(await _accountDao.GetFavorites(this.ContextUserId));
}
[HttpPost("favorites/{knokId}/add")]
public async Task<ApiArray<int>> AddFavorite([FromRoute]int knokId/*, [FromServices] IUidManager uidManager*/)
{
return new ApiArray<int>(await _accountDao.AddFavorite(this.ContextUserId, knokId)/*.Select(it=> uidManager.ToIntUid)*/);
}
[HttpPost("favorites/{knokId}/remove")]
public async Task<ApiArray<int>> RemoveFavorite([FromRoute]int knokId)
{
return new ApiArray<int>(await _accountDao.RemoveFavorite(this.ContextUserId, knokId));
}
[HttpGet("follows/get")]
public async Task<ApiArray<long>> GetFollows()
{
return new ApiArray<long>(await _accountDao.GetFollows(this.ContextUserId));
}
[HttpPost("follows/{userId}/add")]
public async Task<ApiArray<long>> AddFollows([FromRoute]long userId)
{
if (!this.ContextUserId.Equals(userId))
{
return new ApiArray<long>(await _accountDao.AddFollow(this.ContextUserId, userId));
}
throw new ApiResponseException(ApiErrorType.InvalidUsernameOrPassword, $"User {this.ContextUserId} cannot follow himself");
}
[HttpPost("follows/{userId}/remove")]
public async Task<ApiArray<long>> RemoveFollows([FromRoute]long userId)
{
return new ApiArray<long>(await _accountDao.RemoveFollow(this.ContextUserId, userId));
}
}
}