Skip to content

Commit

Permalink
update notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
The-DevBlog committed Mar 1, 2024
1 parent 7a5cf8d commit c7d9ea3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
22 changes: 13 additions & 9 deletions devblog/devblog/Controllers/AccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class AccountsController : ControllerBase
public SignInManager<User> _signInMgr { get; }
public UserManager<User> _userMgr { get; }
public IConfiguration _config;
private readonly INotificationService _notifications;
private readonly IUsernameService _username;
private readonly IEmailService _email;

public AccountsController(SignInManager<User> signInMgr, UserManager<User> usermgr, IConfiguration config, IUsernameService username, IEmailService email)
public AccountsController(INotificationService notifications, SignInManager<User> signInMgr, UserManager<User> usermgr, IConfiguration config, IUsernameService username, IEmailService email)
{
_notifications = notifications;
_userMgr = usermgr;
_signInMgr = signInMgr;
_config = config;
Expand Down Expand Up @@ -78,13 +80,6 @@ public async Task<List<UserInfo>> GetUsers()
return users;
}

public class UserInfo
{
public string? UserName { get; set; }
public string? Email { get; set; }
public bool Subscribed { get; set; }
}

/// <summary>
/// Delete an account
/// </summary>
Expand All @@ -99,7 +94,8 @@ public async Task<IActionResult> DeleteAccount()

if (user != null)
{
// sign current user out and delete account
// sign current user out and delete account & notifications
await _notifications.DeleteAllForUser(username);
await _signInMgr.SignOutAsync();
await _userMgr.DeleteAsync(user);
return Ok();
Expand All @@ -116,6 +112,7 @@ public async Task AdminDeleteAccount(string username)

if (user != null)
{
await _notifications.DeleteAllForUser(username);
await _userMgr.DeleteAsync(user);
}
}
Expand Down Expand Up @@ -264,5 +261,12 @@ private async Task<List<Claim>> GenerateClaims(User user)

return claims;
}

public class UserInfo
{
public string? UserName { get; set; }
public string? Email { get; set; }
public bool Subscribed { get; set; }
}
}
}
6 changes: 6 additions & 0 deletions devblog/devblog/Interfaces/INotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ public interface INotificationService
/// </summary>
/// <param name="postId"></param>
Task DeleteAllForPost(int postId);

/// <summary>
/// Delete all notifications for a specified user
/// </summary>
/// <param name="username"></param>
Task DeleteAllForUser(string username);
}
}
13 changes: 12 additions & 1 deletion devblog/devblog/Services/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task Create(int PostId)
/// <returns>List<Notification></returns>
public async Task<List<Notification>> Get(string userName)
{
var notifications = await _db.Notification.Where(n => n.UserName == userName).ToListAsync();
var notifications = await _db.Notification.Where(n => n.UserName.ToLower() == userName.ToLower()).ToListAsync();
return notifications;
}

Expand All @@ -69,5 +69,16 @@ public async Task DeleteAllForPost(int postId)
notifications.ForEach(n => _db.Remove(n));
await _db.SaveChangesAsync();
}

/// <summary>
/// Delete all notifications for a specified user
/// </summary>
/// <param name="username"></param>
public async Task DeleteAllForUser(string username)
{
var notifications = await _db.Notification.Where(n => n.UserName.ToLower() == username.ToLower()).ToListAsync();
notifications.ForEach(n => _db.Remove(n));
await _db.SaveChangesAsync();
}
}
}
5 changes: 5 additions & 0 deletions devblog/devblog/client/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ pub fn on_click(
});
}

pub fn clear_user_data(dispatch: Dispatch<Store>) {
dispatch.reduce(|_| Store::default().into());
}

pub fn set_user_data(dispatch: Dispatch<Store>, store: Store) {
dispatch.reduce_mut(move |s| {
s.token = store.token;
Expand Down Expand Up @@ -86,6 +90,7 @@ pub fn on_submit(
if let Some(res) = api.fetch(Some(hdrs), body, Method::POST).await {
if res.status() == 200 {
let obj: Store = serde_json::from_str(&res.text().await.unwrap()).unwrap();
log!("FIX ME");
set_user_data(dispatch, obj);
nav.push(&Route::Home);
}
Expand Down
8 changes: 7 additions & 1 deletion devblog/devblog/client/src/pages/sign_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ pub fn sign_out() -> Html {
let nav = use_navigator().unwrap();
let (_, dispatch) = use_store::<Store>();

let sign_out = {
helpers::clear_user_data(dispatch.clone());
helpers::on_submit(&user, nav, Rc::new(Api::SignOut), dispatch)
};

html! {
<form class={style} onsubmit={helpers::on_submit(&user, nav, Rc::new(Api::SignOut), dispatch)}>
// <form class={style} onsubmit={helpers::on_submit(&user, nav, Rc::new(Api::SignOut), dispatch)}>
<form class={style} onsubmit={sign_out}>
<button class="logout">{"Logout"}</button>
</form>
}
Expand Down

0 comments on commit c7d9ea3

Please sign in to comment.