Skip to content

Commit

Permalink
Merge pull request #1 from COMP-1640-GRE/cuongphgch210011
Browse files Browse the repository at this point in the history
Cuongphgch210011
  • Loading branch information
cuongphgch210011 authored Apr 7, 2024
2 parents f604b35 + 90a1101 commit 778ccff
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion DotnetGRPC/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using DotnetGRPC.Model;
using Microsoft.EntityFrameworkCore;

public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
Expand All @@ -9,4 +8,5 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<Template> Template { get; set; }
public DbSet<Notification> Notification { get; set; }
public DbSet<User> User { get; set; }

}
2 changes: 2 additions & 0 deletions DotnetGRPC/Model/Faculty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ public class Faculty
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("id")]
public long Id { get; set; }

[Required]
[Column("name")]
public string Name { get; set; }

[Required]
Expand Down
29 changes: 12 additions & 17 deletions DotnetGRPC/Model/User.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace DotnetGRPC.Model
{
[Table("user")]
Expand All @@ -26,33 +25,26 @@ public class User

[Required]
[Column("role")]
public RoleType Role { get; set; }
public string Role { get; set; }

Check warning on line 28 in DotnetGRPC/Model/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Role' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public enum RoleType
{
Administrator, Student, Guest
}
// public enum RoleType
// {
// Administrator, Student, Guest
// }

[Required]
[Column("email")]
public string Email { get; set; }

Check warning on line 37 in DotnetGRPC/Model/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Email' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[Required]
[Column("first_name")]
public string FirstName { get; set; }
public string? FirstName { get; set; }

[Required]
[Column("last_name")]
public string LastName { get; set; }
public string? LastName { get; set; }

[Required]
[Column("account_status")]
public AccountType AccountStatus { get; set; }

public enum AccountType
{
Active, Locked, Inactive
}
public string AccountStatus { get; set; }

Check warning on line 47 in DotnetGRPC/Model/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'AccountStatus' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[Required]
[Column("created_at")]
Expand All @@ -64,5 +56,8 @@ public enum AccountType

[ForeignKey("FacultyId")]
public Faculty Faculty { get; set; }

Check warning on line 58 in DotnetGRPC/Model/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Faculty' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[Column("faculty_id")]
public long? FacultyId { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion DotnetGRPC/Protos
Submodule Protos updated 1 files
+5 −6 notification.proto
4 changes: 0 additions & 4 deletions DotnetGRPC/Repository/TemplateRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using Microsoft.EntityFrameworkCore;

public class TemplateRepository


{
private readonly AppDbContext _context;

Expand All @@ -16,6 +14,4 @@ public async Task<Template> FindByTemplateCodeAsync(string templateCode)
{
return await _context.Template.FirstOrDefaultAsync(t => t.TemplateCode == templateCode);

Check warning on line 15 in DotnetGRPC/Repository/TemplateRepository.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 15 in DotnetGRPC/Repository/TemplateRepository.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

// Add other methods as needed
}
3 changes: 2 additions & 1 deletion DotnetGRPC/Repository/UserRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DotnetGRPC.Model;
using Microsoft.EntityFrameworkCore;

public class UserRepository
{
Expand All @@ -11,6 +12,6 @@ public UserRepository(AppDbContext context)

public async Task<User> FindByIdAsync(long id)
{
return await _context.User.FindAsync(id);
return await _context.User.SingleOrDefaultAsync(user => user.Id == id);

Check warning on line 15 in DotnetGRPC/Repository/UserRepository.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 15 in DotnetGRPC/Repository/UserRepository.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}
}
59 changes: 32 additions & 27 deletions DotnetGRPC/Services/NotificationService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Net.Mail;
using DotnetGRPC.Model.DTO;
using DotnetGRPC.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Grpc.Core;
using System.Net;
Expand All @@ -27,26 +25,24 @@ public NotificationService(AppDbContext dbContext, IOptions<EmailSettings> email
_notificationRepository = notificationRepository;
}

public async Task SendEmailAsync(long studentUserId, string templateCode, long staffUserId, string option)
public async Task SendEmailAsync(long studentUserId, string templateCode, string option)
{
if (studentUserId == 0)
{
throw new ArgumentException("Student is null");
}

var student = await _userRepository.FindByIdAsync(studentUserId);
var staff = await _userRepository.FindByIdAsync(staffUserId);
var template = await _templateRepository.FindByTemplateCodeAsync(templateCode);
var student = await _userRepository.FindByIdAsync(studentUserId) ?? throw new ArgumentException("Student not found");
var template = await _templateRepository.FindByTemplateCodeAsync(templateCode) ?? throw new ArgumentException("Template not found");
using (var mailMessage = new MailMessage())
{
mailMessage.To.Add(new MailAddress(student.Email));
mailMessage.From = new MailAddress(staff.Email);
mailMessage.From = new MailAddress("[email protected]");
mailMessage.Subject = template.TemplateName;
mailMessage.Body = template.TemplateContent
.Replace("[Student Name]", $"{student.FirstName} {student.LastName}")
.Replace("[Name]", $"{staff.FirstName} {staff.LastName}")
.Replace("[Role]", staff.Role.ToString())
.Replace("[choose appropriate option]", option);
.Replace("[choose appropriate option]", option)
.Replace("[6-Letter Code]", option);
mailMessage.IsBodyHtml = true;

using (var smtpClient = new SmtpClient(_emailSettings.Host, _emailSettings.Port))
Expand All @@ -60,47 +56,56 @@ public async Task SendEmailAsync(long studentUserId, string templateCode, long s

public override async Task<NotificationResponse> sendNotification(NotificationRequest request, ServerCallContext context)
{
if (request.WithEmail)
{
await SendEmailAsync(request.StudentUserid, request.TemplateCode.Replace("noti", "email"), request.StaffUserid, request.Option);
}
var content = string.Empty;

var template = await _templateRepository.FindByTemplateCodeAsync(request.TemplateCode);
var content = template.TemplateContent.Replace("[choose appropriate option]", request.Option);

var notification = new Model.Notification
if (request.TemplateCode != "reset_pw_email")
{
Content = content,
Seen = false,
UserId = request.StudentUserid,
WithEmail = request.WithEmail
};
if (request.WithEmail)
{
await SendEmailAsync(request.UserId, request.TemplateCode.Replace("noti", "email"), request.Option);
}
var template = await _templateRepository.FindByTemplateCodeAsync(request.TemplateCode);
content = template.TemplateContent.Replace("[choose appropriate option]", request.Option);

await _notificationRepository.SaveAsync(notification);
var notification = new Model.Notification
{
Content = content,
Seen = false,
UserId = request.UserId,
WithEmail = request.WithEmail
};

await _notificationRepository.SaveAsync(notification);
}
else
{
await SendEmailAsync(request.UserId, request.TemplateCode, request.Option);
}
var notificationResponse = new NotificationResponse
{
Content = content,
StudentUserid = notification.UserId,
UserId = request.UserId,
WithEmail = request.WithEmail
};

return notificationResponse;
}

public override async Task getListNotifications(ListNotificationRequest request, IServerStreamWriter<NotificationResponse> responseStream, ServerCallContext context)
{
var notifications = await _notificationRepository.GetNotifications(request.StudentUserid, request.Seen);
var notifications = await _notificationRepository.GetNotifications(request.UserId, request.Seen);

foreach (var notification in notifications)
{
var notificationResponse = new NotificationResponse
{
Content = notification.Content,
StudentUserid = notification.UserId,
UserId = notification.UserId,
};

await responseStream.WriteAsync(notificationResponse);
}
}
}
}

0 comments on commit 778ccff

Please sign in to comment.