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 _logger; private readonly IAccountDao _accountDao; private readonly IGeneralManager _generalManager; public AccountController(ILogger logger, IAccountDao accountDao, IGeneralManager generalManager) { _logger = logger; _accountDao = accountDao; _generalManager = generalManager; } [HttpGet("UserAccounts")] public async Task 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 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> GetFavorites() { return new ApiArray(await _accountDao.GetFavorites(this.ContextUserId)); } [HttpPost("favorites/{knokId}/add")] public async Task> AddFavorite([FromRoute]int knokId/*, [FromServices] IUidManager uidManager*/) { return new ApiArray(await _accountDao.AddFavorite(this.ContextUserId, knokId)/*.Select(it=> uidManager.ToIntUid)*/); } [HttpPost("favorites/{knokId}/remove")] public async Task> RemoveFavorite([FromRoute]int knokId) { return new ApiArray(await _accountDao.RemoveFavorite(this.ContextUserId, knokId)); } [HttpGet("follows/get")] public async Task> GetFollows() { return new ApiArray(await _accountDao.GetFollows(this.ContextUserId)); } [HttpPost("follows/{userId}/add")] public async Task> AddFollows([FromRoute]long userId) { if (!this.ContextUserId.Equals(userId)) { return new ApiArray(await _accountDao.AddFollow(this.ContextUserId, userId)); } throw new ApiResponseException(ApiErrorType.InvalidUsernameOrPassword, $"User {this.ContextUserId} cannot follow himself"); } [HttpPost("follows/{userId}/remove")] public async Task> RemoveFollows([FromRoute]long userId) { return new ApiArray(await _accountDao.RemoveFollow(this.ContextUserId, userId)); } } }