101 lines
3.4 KiB
C#
101 lines
3.4 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using qtc_api.Models;
|
|
using qtc_api.Services.ContactService;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Security.Claims;
|
|
|
|
namespace qtc_api.Controllers
|
|
{
|
|
[Route("api/contacts")]
|
|
[ApiController]
|
|
public class ContactController : ControllerBase
|
|
{
|
|
private IContactService _contactService;
|
|
private IHubContext<ChatHub> _chatGwContext;
|
|
|
|
public ContactController(IContactService contactService, IHubContext<ChatHub> chatGwContext)
|
|
{
|
|
_contactService = contactService;
|
|
_chatGwContext = chatGwContext;
|
|
}
|
|
|
|
[HttpPost("add-contact")]
|
|
[Authorize]
|
|
public async Task<ActionResult<ServiceResponse<Contact>>> AddContact(string userId)
|
|
{
|
|
var identity = HttpContext.User.Identity as ClaimsIdentity;
|
|
|
|
if (identity != null)
|
|
{
|
|
IEnumerable<Claim> claims = identity.Claims;
|
|
var ownerId = claims.First().Value;
|
|
|
|
var result = await _contactService.CreateContact(ownerId, userId);
|
|
|
|
// refresh contacts list for both users
|
|
await _chatGwContext.Clients.User(userId).SendAsync("RefreshContactsList");
|
|
await _chatGwContext.Clients.User(ownerId).SendAsync("RefreshContactsList");
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
return Unauthorized();
|
|
}
|
|
|
|
[HttpPost("approve-contact")]
|
|
[Authorize]
|
|
public async Task<ActionResult<ServiceResponse<bool>>> ApproveContact(string userId)
|
|
{
|
|
var identity = HttpContext.User.Identity as ClaimsIdentity;
|
|
|
|
if (identity != null)
|
|
{
|
|
IEnumerable<Claim> claims = identity.Claims;
|
|
var ownerId = claims.First().Value;
|
|
|
|
var result = await _contactService.UpdateContactStatus(ownerId, userId, Contact.ContactStatus.Accepted, Contact.ContactStatus.Accepted);
|
|
|
|
// refresh contacts list for both users
|
|
await _chatGwContext.Clients.User(userId).SendAsync("RefreshContactsList");
|
|
await _chatGwContext.Clients.User(ownerId).SendAsync("RefreshContactsList");
|
|
|
|
return Ok(result);
|
|
}
|
|
|
|
return Unauthorized();
|
|
}
|
|
|
|
[HttpDelete("remove-contact")]
|
|
[Authorize]
|
|
public async Task<ActionResult<ServiceResponse<Contact>>> RemoveContact(string userId)
|
|
{
|
|
var identity = HttpContext.User.Identity as ClaimsIdentity;
|
|
|
|
if (identity != null)
|
|
{
|
|
IEnumerable<Claim> claims = identity.Claims;
|
|
var ownerId = claims.First().Value;
|
|
|
|
var response = await _contactService.DeleteContact(ownerId, userId);
|
|
|
|
// refresh contacts list for both users
|
|
await _chatGwContext.Clients.User(userId).SendAsync("RefreshContactsList");
|
|
await _chatGwContext.Clients.User(ownerId).SendAsync("RefreshContactsList");
|
|
|
|
return Ok(response);
|
|
}
|
|
|
|
return Unauthorized();
|
|
}
|
|
|
|
[HttpGet("get-user-contacts")]
|
|
[Authorize]
|
|
public async Task<ActionResult<ServiceResponse<List<Contact>>>> GetUserContacts(User user)
|
|
{
|
|
var result = await _contactService.GetUserContacts(user);
|
|
return Ok(result);
|
|
}
|
|
}
|
|
}
|