234 lines
9.6 KiB
C#
234 lines
9.6 KiB
C#
using Common.Dtos.Conversation;
|
|
using Common.Enums;
|
|
using Hushian.Application.Constants;
|
|
using Hushian.Application.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Hushian.WebApi.Controllers.v1
|
|
{
|
|
[Route("api/v1/[controller]")]
|
|
[ApiController]
|
|
public class ChatController : ControllerBase
|
|
{
|
|
private readonly ChatService _chatService; private readonly ExperService _experService;
|
|
|
|
public ChatController(ChatService chatService, ExperService experService)
|
|
{
|
|
_chatService = chatService;
|
|
_experService = experService;
|
|
}
|
|
|
|
[HttpPost("MyChats")]
|
|
[Authorize(Roles = "Company,Exper")]
|
|
public async Task<ActionResult> MyChats([FromBody] ConversationStatus status)
|
|
{
|
|
if (User.IsInRole("Exper"))
|
|
{
|
|
string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
int ExperID = Convert.ToInt32(strExperID);
|
|
var response = await _chatService.GeChatsByExperID(ExperID, status);
|
|
return Ok(response);
|
|
}
|
|
else if (User.IsInRole("Company"))
|
|
{
|
|
string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
int CompanyID = Convert.ToInt32(strCompanyID);
|
|
|
|
var response = await _chatService.GetChatsByCompanyID(CompanyID, status);
|
|
return Ok(response);
|
|
}
|
|
return Forbid();
|
|
|
|
}
|
|
[HttpGet("User/LastOpenChatInCompany/{CompanyID}")]
|
|
[Authorize(Roles = "User")]
|
|
public async Task<ActionResult> GetLastOpenChatInCompany(int CompanyID)
|
|
{
|
|
string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
int UserID = Convert.ToInt32(strUserID);
|
|
var response = await _chatService.GetLastOpenChatInCompany(CompanyID,UserID);
|
|
|
|
return response.Success ? Ok(response.Value) : Accepted() ;
|
|
|
|
}
|
|
[HttpGet("User/Chat/{ChatID}")]
|
|
[Authorize(Roles = "User")]
|
|
public async Task<ActionResult> Getchat(int ChatID)
|
|
{
|
|
string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
int UserID = Convert.ToInt32(strUserID);
|
|
var response = await _chatService.GetChat(ChatID, UserID);
|
|
|
|
return response.Success ? Ok(response.Value) : Accepted();
|
|
|
|
}
|
|
[HttpGet("ChatsAwaitingOurResponse")]
|
|
[Authorize(Roles = "Company,Exper")]
|
|
public async Task<ActionResult> ChatsAwaitingOurResponse()
|
|
{
|
|
int CompanyID = 0;
|
|
int? ExperID = null;
|
|
if (User.IsInRole("Exper"))
|
|
{
|
|
string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
ExperID = Convert.ToInt32(strExperID);
|
|
|
|
CompanyID = await _experService.GetCompanyIDExper(ExperID.Value);
|
|
}
|
|
else if (User.IsInRole("Company"))
|
|
{
|
|
string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
CompanyID = Convert.ToInt32(strCompanyID);
|
|
}
|
|
|
|
var response = await _chatService.ChatsAwaitingOurResponse(CompanyID,ExperID);
|
|
return Ok(response);
|
|
}
|
|
[HttpPut("Attached-to/{toExperID}/{ChatID}")]
|
|
[Authorize(Roles = "Company,Exper")]
|
|
public async Task<ActionResult> Attachedto(int toExperID,int ChatID)
|
|
{
|
|
int CompanyID = 0;
|
|
int? ExperID = null;
|
|
if (User.IsInRole("Exper"))
|
|
{
|
|
string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
ExperID = Convert.ToInt32(strExperID);
|
|
|
|
CompanyID = await _experService.GetCompanyIDExper(ExperID.Value);
|
|
}
|
|
else if (User.IsInRole("Company"))
|
|
{
|
|
string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
CompanyID = Convert.ToInt32(strCompanyID);
|
|
}
|
|
|
|
var response = await _chatService.Attachedto(ChatID, ExperID,toExperID,CompanyID);
|
|
return response ?NoContent(): BadRequest(new List<string> { "خطا در بروزرسانی وضعیت" });
|
|
}
|
|
[HttpPost("NewChatFromCurrentUser")]
|
|
public async Task<ActionResult> NewChatFromCurrentUser(ADD_ConversationDto conversation)
|
|
{
|
|
conversation.UserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => Convert.ToInt32(s.Value)).First();
|
|
var response = await _chatService.NewChat(conversation);
|
|
return response.Success ? Ok(response.Value)
|
|
: BadRequest(response.Errors);
|
|
}
|
|
[HttpPost("ADDChatResponse")]
|
|
[Authorize(Roles = "Company,User,Exper")]
|
|
public async Task<ActionResult> ADDChatResponse([FromBody] ADD_ConversationResponseDto ConversationItem)
|
|
{
|
|
|
|
int? ExperID = null;
|
|
if (User.IsInRole("Exper"))
|
|
{
|
|
string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
ExperID = Convert.ToInt32(strExperID);
|
|
ConversationItem.Type = ConversationType.EU;
|
|
}
|
|
|
|
else if (User.IsInRole("User"))
|
|
{
|
|
ConversationItem.Type = ConversationType.UE;
|
|
}
|
|
else if (User.IsInRole("Company"))
|
|
{
|
|
ConversationItem.Type = ConversationType.CU;
|
|
|
|
}
|
|
else return Unauthorized();
|
|
|
|
|
|
var Response = await _chatService.ADDChatResponse(ConversationItem, ExperID);
|
|
return Response.Success ? Ok(Response.Value)
|
|
: BadRequest(Response.Errors);
|
|
}
|
|
[HttpPut("ChatIsFinish/{ChatID}")]
|
|
[Authorize(Roles = "Company,Exper")]
|
|
public async Task<ActionResult> ChatIsFinish(int ChatID)
|
|
{
|
|
|
|
int CompanyID = 0;
|
|
int? ExperID = null;
|
|
if (User.IsInRole("Exper"))
|
|
{
|
|
string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
ExperID = Convert.ToInt32(strExperID);
|
|
|
|
CompanyID = await _experService.GetCompanyIDExper(ExperID.Value);
|
|
}
|
|
else if (User.IsInRole("Company"))
|
|
{
|
|
string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
CompanyID = Convert.ToInt32(strCompanyID);
|
|
}
|
|
|
|
return await _chatService.FinishChat(ChatID, CompanyID, ExperID) ? NoContent()
|
|
: BadRequest(new List<string> { "خطا در بروزرسانی وضعیت" });
|
|
}
|
|
[HttpPut("OpenChat/{ChatID}")]
|
|
[Authorize(Roles = "Company,Exper")]
|
|
public async Task<ActionResult> OpenChat(int ChatID)
|
|
{
|
|
|
|
int CompanyID = 0;
|
|
int? ExperID =null;
|
|
if (User.IsInRole("Exper"))
|
|
{
|
|
string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
ExperID = Convert.ToInt32(strExperID);
|
|
|
|
CompanyID = await _experService.GetCompanyIDExper(ExperID.GetValueOrDefault());
|
|
}
|
|
else if (User.IsInRole("Company"))
|
|
{
|
|
string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
CompanyID = Convert.ToInt32(strCompanyID);
|
|
}
|
|
|
|
return await _chatService.OpenChat(ChatID, CompanyID, ExperID) ? NoContent()
|
|
: BadRequest(new List<string> { "خطا در بروزرسانی وضعیت" });
|
|
}
|
|
[HttpPut("ChatIsFinishFromUser/{ChatID}")]
|
|
[Authorize(Roles = "User")]
|
|
public async Task<ActionResult> ChatIsFinishFromUser(int ChatID)
|
|
{
|
|
string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
int UserID = Convert.ToInt32(strUserID);
|
|
|
|
|
|
return await _chatService.FinishChatFromUser(ChatID, UserID) ? NoContent()
|
|
: BadRequest(new List<string> { "خطا در بروزرسانی وضعیت" });
|
|
}
|
|
[HttpPut("MarkAsReadChatItem/{ConversationItemID}")]
|
|
[Authorize(Roles = "Company,User,Exper")]
|
|
public async Task<ActionResult> MarkAsReadChatItem(int ConversationItemID)
|
|
{
|
|
int? ID = null;
|
|
ConversationType Type = ConversationType.UE;
|
|
if (User.IsInRole("Exper"))
|
|
{
|
|
string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
|
|
ID = Convert.ToInt32(strExperID);
|
|
Type = ConversationType.EU;
|
|
}
|
|
|
|
else if (User.IsInRole("User"))
|
|
{
|
|
Type = ConversationType.UE;
|
|
}
|
|
else if (User.IsInRole("Company"))
|
|
{
|
|
Type = ConversationType.CU;
|
|
}
|
|
else return Unauthorized();
|
|
|
|
return await _chatService.MarkAsReadChatItem(ConversationItemID, Type, ID) ? NoContent()
|
|
: BadRequest(new List<string>() { "خطا در بروزرسانی گفتگو" });
|
|
|
|
}
|
|
}
|
|
}
|