diff --git a/Common/Dtos/Prompt/ADD_PromptDto.cs b/Common/Dtos/Prompt/ADD_PromptDto.cs new file mode 100644 index 0000000..f782613 --- /dev/null +++ b/Common/Dtos/Prompt/ADD_PromptDto.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Common.Dtos.Prompt +{ + public class ADD_PromptDto + { + public string Test { get; set; } + } +} + + diff --git a/Common/Dtos/Prompt/ReadANDUpdate_PromptDto.cs b/Common/Dtos/Prompt/ReadANDUpdate_PromptDto.cs new file mode 100644 index 0000000..ab255f3 --- /dev/null +++ b/Common/Dtos/Prompt/ReadANDUpdate_PromptDto.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Common.Dtos.Prompt +{ + public class ReadANDUpdate_PromptDto + { + public int ID { get; set; } + public DateTime Cdatetime { get; set; } + public int CompanyID { get; set; } + public string Test { get; set; } + } +} + + diff --git a/Hushian.Application/ApplicationServicesRegistration.cs b/Hushian.Application/ApplicationServicesRegistration.cs index 927bcd0..346d7d4 100644 --- a/Hushian.Application/ApplicationServicesRegistration.cs +++ b/Hushian.Application/ApplicationServicesRegistration.cs @@ -66,6 +66,7 @@ namespace Hushian.Application services.AddScoped(typeof(ChatService)); services.AddScoped(typeof(ExperService)); services.AddScoped(typeof(GroupService)); + services.AddScoped(typeof(PromptService)); services.AddScoped(typeof(UserService)); services.AddScoped(typeof(VerificationService)); services.Configure(configuration.GetSection("JwtSettings")); diff --git a/Hushian.Application/MappingProfile.cs b/Hushian.Application/MappingProfile.cs index 00c035f..11b1fe7 100644 --- a/Hushian.Application/MappingProfile.cs +++ b/Hushian.Application/MappingProfile.cs @@ -2,6 +2,7 @@ using Common.Dtos.Company; using Common.Dtos.Exper; using Common.Dtos.Group; +using Common.Dtos.Prompt; using Hushian.Domain.Entites; @@ -17,6 +18,8 @@ namespace Hushian.Application CreateMap().ReverseMap(); CreateMap(); CreateMap(); + CreateMap(); + CreateMap().ReverseMap(); } } } diff --git a/Hushian.Application/Services/PromptService.cs b/Hushian.Application/Services/PromptService.cs new file mode 100644 index 0000000..097ad7a --- /dev/null +++ b/Hushian.Application/Services/PromptService.cs @@ -0,0 +1,93 @@ +using AutoMapper; +using Common.Dtos.Prompt; +using Hushian.Application.Contracts.Persistence; +using Hushian.Application.Models; +using Hushian.Domain.Entites; +using Microsoft.EntityFrameworkCore; + +namespace Hushian.Application.Services +{ + public class PromptService + { + private readonly IGenericRepository _promptRepository; + private readonly IMapper _mapper; + + public PromptService(IGenericRepository promptRepository, IMapper mapper) + { + _promptRepository = promptRepository; + _mapper = mapper; + } + + public async Task> AddPrompt(ADD_PromptDto model, int CompanyID) + { + ResponseBase response = new(); + try + { + var entity = _mapper.Map(model); + entity.CompanyID = CompanyID; + entity.Cdatetime = DateTime.Now; + response.Value = response.Success = await _promptRepository.ADDBool(entity); + } + catch (Exception) + { + response.Errors.Add("خطا در ذخیره سازی"); + } + return response; + } + + public async Task GetPrompt(int id, int CompanyID) + { + var entity = await _promptRepository.Get().FirstOrDefaultAsync(f => f.ID == id && f.CompanyID == CompanyID); + return _mapper.Map(entity); + } + + public async Task> GetCompanyPrompts(int CompanyID) + { + var list = await _promptRepository.Get().Where(w => w.CompanyID == CompanyID).OrderByDescending(o=>o.ID).ToListAsync(); + return _mapper.Map>(list); + } + + public async Task> UpdatePrompt(ReadANDUpdate_PromptDto model, int CompanyID) + { + ResponseBase response = new(); + try + { + var entity = await _promptRepository.Get().FirstOrDefaultAsync(f => f.ID == model.ID && f.CompanyID == CompanyID); + if (entity == null) + { + response.Errors.Add("یافت نشد"); + return response; + } + entity.Test = model.Test; + response.Value = response.Success = await _promptRepository.UPDATEBool(entity); + } + catch (Exception) + { + response.Errors.Add("خطای سیستمی"); + } + return response; + } + + public async Task> DeletePrompt(int id, int CompanyID) + { + ResponseBase response = new(); + try + { + var entity = await _promptRepository.Get().FirstOrDefaultAsync(f => f.ID == id && f.CompanyID == CompanyID); + if (entity == null) + { + response.Errors.Add("یافت نشد"); + return response; + } + response.Value = response.Success = await _promptRepository.DELETE(entity); + } + catch (Exception) + { + response.Errors.Add("خطای سیستمی"); + } + return response; + } + } +} + + diff --git a/Presentation/Hushian.WebApi/Controllers/v1/PromptController.cs b/Presentation/Hushian.WebApi/Controllers/v1/PromptController.cs new file mode 100644 index 0000000..ca1d1f8 --- /dev/null +++ b/Presentation/Hushian.WebApi/Controllers/v1/PromptController.cs @@ -0,0 +1,74 @@ +using Common.Dtos.Prompt; +using Hushian.Application.Constants; +using Hushian.Application.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace Hushian.WebApi.Controllers.v1 +{ + [Route("api/v1/[controller]")] + [ApiController] + public class PromptController : ControllerBase + { + private readonly PromptService _promptService; + public PromptController(PromptService promptService) + { + _promptService = promptService; + } + + [HttpPost("AddPrompt")] + [Authorize(Roles = "Company")] + public async Task AddPrompt([FromBody] ADD_PromptDto model) + { + string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); + int CompanyID = Convert.ToInt32(strCompanyID); + var response = await _promptService.AddPrompt(model, CompanyID); + return response.Success && response.Value ? NoContent() + : BadRequest(response.Errors); + } + + [HttpPut("UpdatePrompt")] + [Authorize(Roles = "Company")] + public async Task UpdatePrompt([FromBody] ReadANDUpdate_PromptDto model) + { + string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); + int CompanyID = Convert.ToInt32(strCompanyID); + var response = await _promptService.UpdatePrompt(model, CompanyID); + return response.Success && response.Value ? NoContent() + : BadRequest(response.Errors); + } + + [HttpDelete("DeletePrompt/{id}")] + [Authorize(Roles = "Company")] + public async Task DeletePrompt(int id) + { + string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); + int CompanyID = Convert.ToInt32(strCompanyID); + var response = await _promptService.DeletePrompt(id, CompanyID); + return response.Success && response.Value ? NoContent() + : BadRequest(response.Errors); + } + + [HttpGet("GetPrompt/{id}")] + [Authorize(Roles = "Company")] + public async Task> GetPrompt(int id) + { + string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); + int CompanyID = Convert.ToInt32(strCompanyID); + var response = await _promptService.GetPrompt(id, CompanyID); + return response != null ? Ok(response) : NotFound(); + } + + [HttpGet("GetPrompts")] + [Authorize(Roles = "Company")] + public async Task>> GetPrompts() + { + string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); + int CompanyID = Convert.ToInt32(strCompanyID); + var response = await _promptService.GetCompanyPrompts(CompanyID); + return Ok(response); + } + } +} + +