diff --git a/Common/Dtos/aiNewResponseDto.cs b/Common/Dtos/aiNewResponseDto.cs new file mode 100644 index 0000000..9868020 --- /dev/null +++ b/Common/Dtos/aiNewResponseDto.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Common.Dtos +{ + public class aiNewResponseDto + { + public int companyId { get; set; } + public int userId { get; set; } + public string requestText { get; set; } + } + public class aiResponseDto + { + public DateTime dateTime { get; set; } + public string requestText { get; set; } + public string responseText { get; set; } + } +} diff --git a/Hushian.Application/ApplicationServicesRegistration.cs b/Hushian.Application/ApplicationServicesRegistration.cs index 346d7d4..7b4ef17 100644 --- a/Hushian.Application/ApplicationServicesRegistration.cs +++ b/Hushian.Application/ApplicationServicesRegistration.cs @@ -69,6 +69,7 @@ namespace Hushian.Application services.AddScoped(typeof(PromptService)); services.AddScoped(typeof(UserService)); services.AddScoped(typeof(VerificationService)); + services.AddScoped(typeof(AIService)); services.Configure(configuration.GetSection("JwtSettings")); } } diff --git a/Hushian.Application/Services/AIService.cs b/Hushian.Application/Services/AIService.cs new file mode 100644 index 0000000..652f74e --- /dev/null +++ b/Hushian.Application/Services/AIService.cs @@ -0,0 +1,72 @@ +using AutoMapper; +using Common.Dtos; +using Hushian.Application.Contracts.Persistence; +using Hushian.Application.Models; +using Hushian.Domain.Entites; +using Microsoft.EntityFrameworkCore; +using System.Linq; + +namespace Hushian.Application.Services +{ + public class AIService + { + private readonly IGenericRepository _aiaRepository; + private readonly IMapper _mapper; + + public AIService(IGenericRepository aiaRepository, IMapper mapper) + { + _aiaRepository = aiaRepository; + _mapper = mapper; + } + + public async Task> NewRequest + (aiNewResponseDto dto) + { + ResponseBase response = new(); + try + { + string responeai="همین جوری"; + bool sucessresponseai = false; + + + var entity = new AIA + { + CompanyID = dto.companyId, + UserID = dto.userId, + Request = dto.requestText, + Response = responeai, + Cdatetime = DateTime.Now + }; + var added = await _aiaRepository.ADD(entity); + + if(sucessresponseai) + response.Value = new() { dateTime=added.Cdatetime,responseText=added.Response,requestText=added.Request}; + response.Success = sucessresponseai; + } + catch (Exception) + { + response.Errors.Add("خطا در ذخیره سازی"); + } + return response; + } + + public async Task> GetCurrent(int companyId, int userId) + { + return await _aiaRepository + .Get() + .Where(w => w.CompanyID == companyId && w.UserID == userId && w.Cdatetime.AddHours(3) >= DateTime.Now) + .OrderBy(o => o.ID) + .Select(s=>new aiResponseDto() { responseText = s.Response, requestText = s.Request,dateTime=s.Cdatetime}) + .ToListAsync(); + } + + //public async Task DeleteEntry(int id, int companyId) + //{ + // var entity = await _aiaRepository.Get().FirstOrDefaultAsync(f => f.ID == id && f.CompanyID == companyId); + // if (entity == null) return false; + // return await _aiaRepository.DELETE(entity); + //} + } +} + + diff --git a/Hushian.Domain/Entites/AIA.cs b/Hushian.Domain/Entites/AIA.cs index afefb07..ee1c2e1 100644 --- a/Hushian.Domain/Entites/AIA.cs +++ b/Hushian.Domain/Entites/AIA.cs @@ -7,10 +7,10 @@ using System.Threading.Tasks; namespace Hushian.Domain.Entites { - class AIA : BaseEntity + public class AIA : BaseEntity { public int ID { get; set; } - public DateTime Cdatetime { get; set; } + public DateTime Cdatetime { get; set; } = DateTime.Now; public int CompanyID { get; set; } public int UserID { get; set; } diff --git a/Infrastructure/Persistence/HushianDbContext.cs b/Infrastructure/Persistence/HushianDbContext.cs index c0052b6..9231667 100644 --- a/Infrastructure/Persistence/HushianDbContext.cs +++ b/Infrastructure/Persistence/HushianDbContext.cs @@ -23,6 +23,7 @@ namespace Hushian.Persistence public DbSet Users { get; set; } public DbSet verificationCodes { get; set; } public DbSet prompts { get; set; } + public DbSet AIAs { get; set; } #endregion protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/Infrastructure/Persistence/Migrations/20250817205731_newtable.Designer.cs b/Infrastructure/Persistence/Migrations/20250817205731_newtable.Designer.cs new file mode 100644 index 0000000..05fe624 --- /dev/null +++ b/Infrastructure/Persistence/Migrations/20250817205731_newtable.Designer.cs @@ -0,0 +1,520 @@ +// +using System; +using Hushian.Persistence; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Hushian.Persistence.Migrations +{ + [DbContext(typeof(HushianDbContext))] + [Migration("20250817205731_newtable")] + partial class newtable + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.7") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Hushian.Domain.Entites.AIA", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.Property("CompanyID") + .HasColumnType("int"); + + b.Property("Request") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Response") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.ToTable("AIAs"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Company", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Available") + .HasColumnType("bit"); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.Property("Email") + .HasColumnType("nvarchar(max)"); + + b.Property("FullName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FullNameManager") + .HasColumnType("nvarchar(max)"); + + b.Property("Mobile") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Verified") + .HasColumnType("bit"); + + b.Property("WebSite") + .HasColumnType("nvarchar(max)"); + + b.Property("allowBot") + .HasColumnType("bit"); + + b.Property("logo") + .HasColumnType("varbinary(max)"); + + b.HasKey("ID"); + + b.ToTable("Companies"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.CompanyContentInfo", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.Property("CompanyID") + .HasColumnType("int"); + + b.Property("Content") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.HasIndex("CompanyID"); + + b.ToTable("CompanyContentInfo"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Conversation", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.Property("CompanyID") + .HasColumnType("int"); + + b.Property("FinishedDateTime") + .HasColumnType("datetime2"); + + b.Property("GroupID") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("UserID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.HasIndex("CompanyID"); + + b.HasIndex("GroupID"); + + b.HasIndex("UserID"); + + b.ToTable("Conversations"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.ConversationResponse", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.Property("ConversationID") + .HasColumnType("int"); + + b.Property("ExperID") + .HasColumnType("int"); + + b.Property("FileContent") + .HasColumnType("varbinary(max)"); + + b.Property("FileName") + .HasColumnType("nvarchar(max)"); + + b.Property("FileType") + .HasColumnType("nvarchar(max)"); + + b.Property("IsRead") + .HasColumnType("bit"); + + b.Property("ReadDateTime") + .HasColumnType("datetime2"); + + b.Property("Text") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Type") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.HasIndex("ConversationID"); + + b.HasIndex("ExperID"); + + b.ToTable("ConversationItems"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Exper", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Available") + .HasColumnType("bit"); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.Property("CompanyID") + .HasColumnType("int"); + + b.Property("FullName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.HasIndex("CompanyID"); + + b.ToTable("Expers"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.ExperGroup", b => + { + b.Property("ExperID") + .HasColumnType("int"); + + b.Property("GroupID") + .HasColumnType("int"); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.HasKey("ExperID", "GroupID"); + + b.HasIndex("GroupID"); + + b.ToTable("EG"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Group", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Available") + .HasColumnType("bit"); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.Property("CompanyID") + .HasColumnType("int"); + + b.Property("Info") + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("img") + .HasColumnType("varbinary(max)"); + + b.HasKey("ID"); + + b.HasIndex("CompanyID"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Prompt", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.Property("CompanyID") + .HasColumnType("int"); + + b.Property("Test") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.HasIndex("CompanyID"); + + b.ToTable("prompts"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.User", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.Property("FullName") + .HasColumnType("nvarchar(max)"); + + b.Property("Mobile") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Identity.Models.VerificationCode", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.Property("Code") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Mobile") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Type") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.ToTable("verificationCodes"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.CompanyContentInfo", b => + { + b.HasOne("Hushian.Domain.Entites.Company", "company") + .WithMany("CompanyContentInfos") + .HasForeignKey("CompanyID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("company"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Conversation", b => + { + b.HasOne("Hushian.Domain.Entites.Company", "Company") + .WithMany("Conversations") + .HasForeignKey("CompanyID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Hushian.Domain.Entites.Group", "Group") + .WithMany() + .HasForeignKey("GroupID"); + + b.HasOne("Hushian.Domain.Entites.User", "User") + .WithMany("Conversations") + .HasForeignKey("UserID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Company"); + + b.Navigation("Group"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.ConversationResponse", b => + { + b.HasOne("Hushian.Domain.Entites.Conversation", "conversation") + .WithMany("ConversationResponses") + .HasForeignKey("ConversationID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Hushian.Domain.Entites.Exper", "Exper") + .WithMany() + .HasForeignKey("ExperID"); + + b.Navigation("Exper"); + + b.Navigation("conversation"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Exper", b => + { + b.HasOne("Hushian.Domain.Entites.Company", "Company") + .WithMany("Expers") + .HasForeignKey("CompanyID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Company"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.ExperGroup", b => + { + b.HasOne("Hushian.Domain.Entites.Exper", "Exper") + .WithMany("EG") + .HasForeignKey("ExperID") + .OnDelete(DeleteBehavior.ClientCascade) + .IsRequired(); + + b.HasOne("Hushian.Domain.Entites.Group", "Group") + .WithMany("EG") + .HasForeignKey("GroupID") + .OnDelete(DeleteBehavior.ClientCascade) + .IsRequired(); + + b.Navigation("Exper"); + + b.Navigation("Group"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Group", b => + { + b.HasOne("Hushian.Domain.Entites.Company", "Company") + .WithMany("Groups") + .HasForeignKey("CompanyID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Company"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Prompt", b => + { + b.HasOne("Hushian.Domain.Entites.Company", "Company") + .WithMany("prompts") + .HasForeignKey("CompanyID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Company"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Company", b => + { + b.Navigation("CompanyContentInfos"); + + b.Navigation("Conversations"); + + b.Navigation("Expers"); + + b.Navigation("Groups"); + + b.Navigation("prompts"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Conversation", b => + { + b.Navigation("ConversationResponses"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Exper", b => + { + b.Navigation("EG"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.Group", b => + { + b.Navigation("EG"); + }); + + modelBuilder.Entity("Hushian.Domain.Entites.User", b => + { + b.Navigation("Conversations"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Infrastructure/Persistence/Migrations/20250817205731_newtable.cs b/Infrastructure/Persistence/Migrations/20250817205731_newtable.cs new file mode 100644 index 0000000..431a816 --- /dev/null +++ b/Infrastructure/Persistence/Migrations/20250817205731_newtable.cs @@ -0,0 +1,39 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Hushian.Persistence.Migrations +{ + /// + public partial class newtable : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AIAs", + columns: table => new + { + ID = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Cdatetime = table.Column(type: "datetime2", nullable: false), + CompanyID = table.Column(type: "int", nullable: false), + UserID = table.Column(type: "int", nullable: false), + Request = table.Column(type: "nvarchar(max)", nullable: false), + Response = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AIAs", x => x.ID); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AIAs"); + } + } +} diff --git a/Infrastructure/Persistence/Migrations/HushianDbContextModelSnapshot.cs b/Infrastructure/Persistence/Migrations/HushianDbContextModelSnapshot.cs index ebe3a9e..d1b8d3c 100644 --- a/Infrastructure/Persistence/Migrations/HushianDbContextModelSnapshot.cs +++ b/Infrastructure/Persistence/Migrations/HushianDbContextModelSnapshot.cs @@ -22,6 +22,36 @@ namespace Hushian.Persistence.Migrations SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + modelBuilder.Entity("Hushian.Domain.Entites.AIA", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Cdatetime") + .HasColumnType("datetime2"); + + b.Property("CompanyID") + .HasColumnType("int"); + + b.Property("Request") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Response") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UserID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.ToTable("AIAs"); + }); + modelBuilder.Entity("Hushian.Domain.Entites.Company", b => { b.Property("ID") diff --git a/Presentation/Hushian.WebApi/Controllers/v1/ConversationController.cs b/Presentation/Hushian.WebApi/Controllers/v1/ConversationController.cs index c4a9608..2f580f7 100644 --- a/Presentation/Hushian.WebApi/Controllers/v1/ConversationController.cs +++ b/Presentation/Hushian.WebApi/Controllers/v1/ConversationController.cs @@ -1,4 +1,5 @@ -using Common.Dtos.Conversation; +using Common.Dtos; +using Common.Dtos.Conversation; using Common.Enums; using Hushian.Application.Constants; using Hushian.Application.Services; @@ -16,11 +17,13 @@ namespace Hushian.WebApi.Controllers.v1 private readonly ConversationService _conversationService; private readonly CompanyService _companyService; private readonly ExperService _experService; - public ConversationController(ConversationService conversationService, CompanyService companyService, ExperService experService) + private readonly AIService _aIService; + public ConversationController(ConversationService conversationService, CompanyService companyService, ExperService experService, AIService aIService) { _conversationService = conversationService; _companyService = companyService; _experService = experService; + _aIService = aIService; } //* @@ -164,5 +167,23 @@ namespace Hushian.WebApi.Controllers.v1 var response = await _conversationService.GEtConversation(UserID,CompanyID ); return Ok(response) ; } + [HttpPost("ai/NewResponse")] + [Authorize(Roles = "User")] + public async Task ainewresponse(aiNewResponseDto dto) + { + string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); + dto.userId = Convert.ToInt32(strUserID); + var response = await _aIService.NewRequest(dto); + return response.Success ? Ok(response):BadRequest(response.Errors); + } + [HttpGet("ai/CurrentResponse/{companyID}")] + [Authorize(Roles = "User")] + public async Task aiCurrentResponse(int companyID) + { + string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); + var UserId = Convert.ToInt32(strUserID); + var response = await _aIService.GetCurrent(companyID,UserId); + return Ok(response); + } } } diff --git a/Presentation/HushianWebApp/Components/PromptManagerComponent.razor b/Presentation/HushianWebApp/Components/PromptManagerComponent.razor index 09005af..54a283f 100644 --- a/Presentation/HushianWebApp/Components/PromptManagerComponent.razor +++ b/Presentation/HushianWebApp/Components/PromptManagerComponent.razor @@ -95,3 +95,4 @@ } + diff --git a/Presentation/HushianWebApp/Service/PromptService.cs b/Presentation/HushianWebApp/Service/PromptService.cs index 93b6383..3393f4c 100644 --- a/Presentation/HushianWebApp/Service/PromptService.cs +++ b/Presentation/HushianWebApp/Service/PromptService.cs @@ -54,3 +54,4 @@ namespace HushianWebApp.Service } +