Skip to content

Commit

Permalink
#53 | Update code | Notification & deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
LuongXuanNhat committed Dec 28, 2023
1 parent 33770d6 commit 4f98f13
Show file tree
Hide file tree
Showing 43 changed files with 2,621 additions and 427 deletions.
39 changes: 39 additions & 0 deletions VNH.Application/Common/Contants/ConstantNofication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VNH.Application.Common.Contants
{
public static class ConstantNofication
{
// Post
public static string CommentPost(string name)
{
return "[ "+name+" ]" + " đã bình luận một bài viết của bạn";
}
public static string LikePost(string name)
{
return "[ " + name + " ]" + " đã thích một bài viết của bạn";
}

// Forum
public static string CommentAnswer(string name)
{
return "[ " + name + " ]" + " đã bình luận trong câu hỏi của bạn";
}
public static string AnswerTheQuestion(string name)
{
return "[ " + name + " ]" + " đã 'trả lời' một câu hỏi của bạn";
}
public static string LikeQuestion(string name)
{
return "[ " + name + " ]" + " đã tán thành một câu hỏi của bạn";
}
public static string LikeAnswer(string name)
{
return "[ " + name + " ]" + " đã 'tán thành' một câu trả lời của bạn";
}
}
}
9 changes: 9 additions & 0 deletions VNH.Application/Common/Contants/ConstantUrl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

namespace VNH.Application.Common.Contants
{
public static class ConstantUrl
{
public const string UrlPostDetail = "/discover/";
public const string UrlQuestionDetail = "/forum/";
}
}
8 changes: 4 additions & 4 deletions VNH.Application/Common/Contants/SystemConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ public static class SystemConstants
public const string Token = "Token";
public const string BaseAddress = "BaseAddress";

//public const string UrlWeb = "https://tyls.fun/";
//public const string ConnectString = "Data Source=202.92.7.204\\MSSQLSERVER2022, 1438;Initial Catalog=tyls;User Id=toiyeulichsu;Password=MheZDu9$oGbrYlL#S%ApJ^qf;TrustServerCertificate=true; ";
public const string UrlWeb = "https://tyls.fun/";
public const string ConnectString = "Data Source=202.92.7.204\\MSSQLSERVER2022, 1438;Initial Catalog=tyls;User Id=toiyeulichsu;Password=MheZDu9$oGbrYlL#S%ApJ^qf;TrustServerCertificate=true; ";

//public const string UrlWeb = "https://vuanhpham25-001-site1.gtempurl.com/";
//public const string ConnectString = "Data Source=SQL5112.site4now.net;Initial Catalog=db_aa121e_vuanhpham25;User Id=db_aa121e_vuanhpham25_admin;Password=30102002Mai";

public const string UrlWeb = "https://localhost:7138/";
public const string ConnectString = "Data Source=.;Initial Catalog=tyls;Integrated Security=True;Encrypt=true;TrustServerCertificate=true;";
//public const string UrlWeb = "https://localhost:7138/";
//public const string ConnectString = "Data Source=.;Initial Catalog=tyls;Integrated Security=True;Encrypt=true;TrustServerCertificate=true;";
}
}
20 changes: 20 additions & 0 deletions VNH.Application/DTOs/Catalog/Notifications/NotificationDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VNH.Application.DTOs.Catalog.Notifications
{
public class NotificationDto
{
public Guid? Id { get; set; }
public Guid UserId { get; set; }
public Guid? NotificationId { get; set; } = Guid.Empty;
public Guid? IdObject { get; set; }
public string? Content { get; set; }
public DateTime? Date { get; set; }
public string? Url { get; set; }

}
}
2 changes: 1 addition & 1 deletion VNH.Application/Interfaces/Catalog/Forum/IAnswerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IAnswerService
{

Task<ApiResult<List<AnswerQuestionDto>>> GetAnswer(string questionId);
Task<ApiResult<List<AnswerQuestionDto>>> CreateAnswer(AnswerQuestionDto answer);
Task<ApiResult<List<AnswerQuestionDto>>> CreateAnswer(AnswerQuestionDto answer, string? id);
Task<ApiResult<List<AnswerQuestionDto>>> UpdateAnswer(AnswerQuestionDto answer);
Task<ApiResult<string>> DeteleAnswer(string id);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VNH.Application.DTOs.Catalog.Notifications;
using VNH.Application.DTOs.Common.ResponseNotification;
using VNH.Domain.Entities;

namespace VNH.Application.Implement.Catalog.NotificationServices
{
public interface INotificationService
{
Task<ApiResult<List<NotificationDto>>> GetAll(string userId);
Task<ApiResult<NotificationDto>> Add(string title);
Task AddNotificationDetail(NotificationDto notification);
Task<ApiResult<NotificationDto>> Update(NotificationDto notification);
}
}
2 changes: 1 addition & 1 deletion VNH.Application/Interfaces/Catalog/Posts/IPostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface IPostService
Task<ApiResult<NumberReponse>> GetSave(PostFpkDto postFpk);
Task<ApiResult<List<PostResponseDto>>> GetPostByTag(string tag);
Task<ApiResult<List<CommentPostDto>>> GetComment(string postId);
Task<ApiResult<List<CommentPostDto>>> CreateComment(CommentPostDto comment);
Task<ApiResult<List<CommentPostDto>>> CreateComment(CommentPostDto comment, string userId);
Task<ApiResult<List<CommentPostDto>>> UpdateComment(CommentPostDto comment);
Task<ApiResult<List<CommentPostDto>>> DeteleComment(string id);
Task<ApiResult<List<PostResponseDto>>> GetMyPostSaved(string id);
Expand Down
20 changes: 20 additions & 0 deletions VNH.Application/Mappers/NotifiMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VNH.Application.DTOs.Catalog.MultipleChoiceDto;
using VNH.Domain.Entities;
using VNH.Domain;
using VNH.Application.DTOs.Catalog.Notifications;

namespace VNH.Application.Mappers
{
public class NotifiMapper : Profile
{
public NotifiMapper() {
CreateMap<NotificationDetail, NotificationDto>().ReverseMap();
}
}
}
1 change: 0 additions & 1 deletion VNH.Application/VNH.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

<ItemGroup>
<Folder Include="Common\Exceptions\" />
<Folder Include="Common\Contants\" />
<Folder Include="System\" />
</ItemGroup>

Expand Down
12 changes: 1 addition & 11 deletions VNH.Domain/Entities/ExamHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,13 @@ public partial class ExamHistory
public Guid UserId { get; set; }

public float Scores { get; set; }


public int CompletionTime { get; set; }

[Column(TypeName = "datetime")]
public DateTime StarDate { get; set; }



[ForeignKey("MultipleChoiceId")]
[InverseProperty("ExamHistory")]
public virtual MultipleChoice MultipleChoice { get; set; }







}
}
20 changes: 20 additions & 0 deletions VNH.Domain/Entities/ExamHistoryMultipleChoice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VNH.Domain.Entities
{
public class ExamHistoryMultipleChoice
{
[Key]
public Guid Id { get; set; }
public Guid ExamHistoryId { get; set; }
public ExamHistory ExamHistory { get; set; }

public Guid MultipleChoiceId { get; set; }
public MultipleChoice MultipleChoice { get; set; }
}
}
13 changes: 1 addition & 12 deletions VNH.Domain/Entities/MultipleChoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace VNH.Domain.Entities
[Table("MultipleChoise")]
public partial class MultipleChoice
{

[Key]
public Guid Id { get; set; }

Expand All @@ -22,30 +21,20 @@ public partial class MultipleChoice
[Column(TypeName = "datetime")]
public DateTime CreatedAt { get; set; } = DateTime.Now;


[Column(TypeName = "datetime")]
public DateTime? UpdatedAt { get; set; }

public int WorkTime { get; set; }


public Guid UserId { get; set; }




[InverseProperty("MultipleChoice")]
public virtual ICollection<Quiz> Quiz { get; set; }


[ForeignKey("UserId")]
public virtual User User { get; set; }

[InverseProperty("MultipleChoice")]
public virtual ExamHistory ExamHistories { get; set; }




public virtual ICollection<ExamHistory> ExamHistory { get; set; }
}
}
2 changes: 2 additions & 0 deletions VNH.Domain/Entities/NotificationDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public class NotificationDetail
public Guid Id { get; set; }
public Guid NotificationId { get; set; }
public Guid UserId { get; set; }
public Guid? IdObject { get; set; }
public string? Content { get; set; }
public DateTime Date { get; set; }
public string? Url { get; set; }
public Confirm IsRead { get; set; }


Expand Down
4 changes: 3 additions & 1 deletion VNH.Infrastructure/DependencyInjectionInfrastructure.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using VNH.Application.Services.Catalog.Users;
Expand Down Expand Up @@ -38,6 +37,8 @@
using VNH.Infrastructure.Implement.Catalog.ExamHistorys;
using VNH.Application.Interfaces.Catalog.NewsHistory;
using VNH.Infrastructure.Implement.Catalog.NewsHistory;
using VNH.Application.Implement.Catalog.NotificationServices;
using VNH.Infrastructure.Implement.Catalog.NotificationServices;

namespace VNH.Infrastructure
{
Expand Down Expand Up @@ -127,6 +128,7 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
services.AddScoped<IAnswerService, AnswerService>();
services.AddScoped<IMultipleChoiceService, MultipleChoiceService>();
services.AddScoped<IExamHistoryService, ExamHistoryService>();
services.AddScoped<INotificationService, NotificationService>();
services.AddSignalR();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task<ApiResult<ExamHistoryResponseDto>> Create(CreateExamHistoryDt
examhistory.MultipleChoiceId = requestDto.MultipleChoiceId;
examhistory.UserId = requestDto.UserId;
examhistory.StarDate = requestDto.StarDate;
examhistory.Scores = requestDto.Scores;
examhistory.Scores = float.Parse(requestDto.Scores.ToString());
examhistory.CompletionTime = requestDto.CompletionTime;
try
{
Expand Down Expand Up @@ -95,7 +95,7 @@ public async Task<ApiResult<ExamHistoryResponseDto>> Update(CreateExamHistoryDto
return new ApiErrorResult<ExamHistoryResponseDto>("Có lỗi xảy ra khi cập nhập kết quả!");
}
updateExamHistory.StarDate = DateTime.Now;
updateExamHistory.Scores = requestDto.Scores;
updateExamHistory.Scores = float.Parse(requestDto.Scores.ToString());
updateExamHistory.CompletionTime = requestDto.CompletionTime;

try
Expand Down
42 changes: 38 additions & 4 deletions VNH.Infrastructure/Implement/Catalog/Forum/AnswerService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using AutoMapper;
using DocumentFormat.OpenXml.Office2010.Excel;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using VNH.Application.Common.Contants;
using VNH.Application.DTOs.Catalog.Forum.Answer;
using VNH.Application.DTOs.Catalog.Notifications;
using VNH.Application.DTOs.Catalog.Users;
using VNH.Application.DTOs.Common;
using VNH.Application.DTOs.Common.ResponseNotification;
using VNH.Application.Implement.Catalog.NotificationServices;
using VNH.Application.Interfaces.Catalog.Forum;
using VNH.Domain;
using VNH.Infrastructure.Presenters;
Expand All @@ -19,16 +24,16 @@ public class AnswerService : IAnswerService
private readonly VietNamHistoryContext _dataContext;
private readonly IHubContext<ChatSignalR> _answerHubContext;
private readonly IMapper _mapper;

private readonly INotificationService _notificationService;

public AnswerService(UserManager<User> userManager, IHubContext<ChatSignalR> answerHubContext,
IMapper mapper,
VietNamHistoryContext vietNamHistoryContext)
IMapper mapper, VietNamHistoryContext vietNamHistoryContext, INotificationService notificationService)
{
_userManager = userManager;
_dataContext = vietNamHistoryContext;
_mapper = mapper;
_answerHubContext = answerHubContext;
_notificationService = notificationService;
}

private UserShortDto? GetUserShort(List<User> users, Guid? IdUser)
Expand Down Expand Up @@ -111,7 +116,7 @@ public async Task<ApiResult<List<AnswerQuestionDto>>> GetAnswer(string questionI



public async Task<ApiResult<List<AnswerQuestionDto>>> CreateAnswer(AnswerQuestionDto answer)
public async Task<ApiResult<List<AnswerQuestionDto>>> CreateAnswer(AnswerQuestionDto answer, string? id)
{
var question = await _dataContext.Questions.FirstOrDefaultAsync(x => x.Id.Equals(Guid.Parse(answer.QuestionId)) && !x.IsDeleted);
if (question == null)
Expand All @@ -121,9 +126,23 @@ public async Task<ApiResult<List<AnswerQuestionDto>>> CreateAnswer(AnswerQuestio

Answer answerQuestion = _mapper.Map<Answer>(answer);
answerQuestion.QuestionId = question.Id;

_dataContext.Answers.Add(answerQuestion);
await _dataContext.SaveChangesAsync();

var user = await _dataContext.User.FirstOrDefaultAsync(x => x.Id.ToString().Equals(id) && !x.IsDeleted);
var noti = new NotificationDto()
{
Id = Guid.NewGuid(),
UserId = user.Id,
IdObject = question.Id,
Content = ConstantNofication.AnswerTheQuestion(user?.Fullname ?? ""),
Date = DateTime.Now,
Url = ConstantUrl.UrlQuestionDetail,
NotificationId = Guid.NewGuid()
};
await _notificationService.AddNotificationDetail(noti);

var answers = await GetAnswer(answer.QuestionId);
await _answerHubContext.Clients.All.SendAsync("ReceiveAnswer", answers);

Expand Down Expand Up @@ -285,6 +304,21 @@ public async Task<ApiResult<NumberReponse>> VoteConfirmByUser(AnswerFpkDto answe
AnswerId = Guid.Parse(answer.AnswerId),
UserId = Guid.Parse(answer.UserId)
};

var ans = await _dataContext.Answers.FirstOrDefaultAsync(x => !x.IsDeleted && x.Id.ToString() == answer.AnswerId);
var user = await _dataContext.User.FirstOrDefaultAsync(x => x.Id.ToString().Equals(ans.AuthorId) && !x.IsDeleted);
var noti = new NotificationDto()
{
Id = Guid.NewGuid(),
UserId = user.Id,
IdObject = Guid.Parse(answer.QuestionId),
Content = ConstantNofication.CommentAnswer(user?.Fullname ?? ""),
Date = DateTime.Now,
Url = ConstantUrl.UrlQuestionDetail,
NotificationId = Guid.NewGuid()
};
await _notificationService.AddNotificationDetail(noti);

_dataContext.AnswerVotes.Add(answerVote);
await _dataContext.SaveChangesAsync();
var numberVote = await _dataContext.AnswerVotes.Where(x => x.AnswerId == Guid.Parse(answer.AnswerId)).CountAsync();
Expand Down
Loading

0 comments on commit 4f98f13

Please sign in to comment.