using Common; using CommonInfrastructure; using CustomerService.Customer; using CustomerService.Domain.Entity; using CustomerService.Domain.Response; using Microsoft.AspNetCore.Mvc; namespace CustomerApi.Controllers; [ApiController] [Route("customerapi/[controller]")] public class CustomerController : BaseController { private readonly ICustomerService _customerService; public CustomerController(ICustomerService customerService) { _customerService = customerService; } [HttpPost("start")] public async Task> StartConversation([FromBody] StartConversationParam param) { var result = await _customerService.StartConversation(param.UserId, param.UserName); if (result.isOk) return this.ToResult(result.conversation); else return this.ToFailResult(result.msg); } [HttpPost("message")] public async Task> SendMessage([FromBody] SendMessageParam param) { var result = await _customerService.SendMessage(param.ConversationId, param.SenderType, param.SenderId, param.SenderName, param.ContentType, param.Content, param.SenderAvatar); if (result.isOk) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } [HttpGet("messages/{conversationId}")] public async Task> GetMessages(string conversationId) { var result = await _customerService.GetMessages(conversationId); if (result.isOk) return this.ToListResult(result.messages ?? new List()); else return this.ToListFailResult(result.msg); } [HttpGet("conversations")] public async Task> GetConversations([FromQuery] string? customerId) { var result = await _customerService.GetConversations(customerId); if (result.isOk) return this.ToListResult(result.conversations ?? new List()); else return this.ToListFailResult(result.msg); } [HttpGet("my-conversations")] public async Task> GetMyConversations() { var userId = loginUser.UserId; var result = await _customerService.GetUserConversations(userId); if (result.isOk) return this.ToListResult(result.conversations ?? new List()); else return this.ToListFailResult(result.msg); } [HttpPost("end")] public async Task> EndConversation([FromBody] EndConversationParam param) { var result = await _customerService.EndConversation(param.ConversationId); if (result.isOk) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } [HttpGet("online")] public async Task> GetOnlineCustomers() { var result = await _customerService.GetOnlineCustomers(); if (result.isOk) return this.ToListResult(result.customers ?? new List()); else return this.ToListFailResult(result.msg); } [HttpPost("status")] public async Task> UpdateCustomerStatus([FromBody] UpdateStatusParam param) { var result = await _customerService.UpdateCustomerStatus(param.UserId, param.Status); if (result.isOk) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } [HttpPost("mark-read")] public async Task> MarkAsRead([FromBody] MarkReadParam param) { var result = await _customerService.MarkAsRead(param.CustomerId, param.UserId); if (result.isOk) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } [HttpGet("chatlog/{conversationId}")] public async Task> GetChatLog(string conversationId) { var result = await _customerService.GetMessages(conversationId); if (result.isOk) return this.ToListResult(result.messages ?? new List()); else return this.ToListFailResult(result.msg); } } public class StartConversationParam { public string UserId { get; set; } = ""; public string UserName { get; set; } = ""; } public class SendMessageParam { public string ConversationId { get; set; } = ""; public int SenderType { get; set; } public string SenderId { get; set; } = ""; public string SenderName { get; set; } = ""; public string? SenderAvatar { get; set; } public int ContentType { get; set; } public string Content { get; set; } = ""; } public class EndConversationParam { public string ConversationId { get; set; } = ""; } public class UpdateStatusParam { public string UserId { get; set; } = ""; public int Status { get; set; } } public class MarkReadParam { public string CustomerId { get; set; } = ""; public string UserId { get; set; } = ""; }