Skip to content

Commit c7d9ea3

Browse files
committed
update notifications
1 parent 7a5cf8d commit c7d9ea3

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

devblog/devblog/Controllers/AccountsController.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ public class AccountsController : ControllerBase
1818
public SignInManager<User> _signInMgr { get; }
1919
public UserManager<User> _userMgr { get; }
2020
public IConfiguration _config;
21+
private readonly INotificationService _notifications;
2122
private readonly IUsernameService _username;
2223
private readonly IEmailService _email;
2324

24-
public AccountsController(SignInManager<User> signInMgr, UserManager<User> usermgr, IConfiguration config, IUsernameService username, IEmailService email)
25+
public AccountsController(INotificationService notifications, SignInManager<User> signInMgr, UserManager<User> usermgr, IConfiguration config, IUsernameService username, IEmailService email)
2526
{
27+
_notifications = notifications;
2628
_userMgr = usermgr;
2729
_signInMgr = signInMgr;
2830
_config = config;
@@ -78,13 +80,6 @@ public async Task<List<UserInfo>> GetUsers()
7880
return users;
7981
}
8082

81-
public class UserInfo
82-
{
83-
public string? UserName { get; set; }
84-
public string? Email { get; set; }
85-
public bool Subscribed { get; set; }
86-
}
87-
8883
/// <summary>
8984
/// Delete an account
9085
/// </summary>
@@ -99,7 +94,8 @@ public async Task<IActionResult> DeleteAccount()
9994

10095
if (user != null)
10196
{
102-
// sign current user out and delete account
97+
// sign current user out and delete account & notifications
98+
await _notifications.DeleteAllForUser(username);
10399
await _signInMgr.SignOutAsync();
104100
await _userMgr.DeleteAsync(user);
105101
return Ok();
@@ -116,6 +112,7 @@ public async Task AdminDeleteAccount(string username)
116112

117113
if (user != null)
118114
{
115+
await _notifications.DeleteAllForUser(username);
119116
await _userMgr.DeleteAsync(user);
120117
}
121118
}
@@ -264,5 +261,12 @@ private async Task<List<Claim>> GenerateClaims(User user)
264261

265262
return claims;
266263
}
264+
265+
public class UserInfo
266+
{
267+
public string? UserName { get; set; }
268+
public string? Email { get; set; }
269+
public bool Subscribed { get; set; }
270+
}
267271
}
268272
}

devblog/devblog/Interfaces/INotificationService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@ public interface INotificationService
2626
/// </summary>
2727
/// <param name="postId"></param>
2828
Task DeleteAllForPost(int postId);
29+
30+
/// <summary>
31+
/// Delete all notifications for a specified user
32+
/// </summary>
33+
/// <param name="username"></param>
34+
Task DeleteAllForUser(string username);
2935
}
3036
}

devblog/devblog/Services/NotificationService.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async Task Create(int PostId)
4545
/// <returns>List<Notification></returns>
4646
public async Task<List<Notification>> Get(string userName)
4747
{
48-
var notifications = await _db.Notification.Where(n => n.UserName == userName).ToListAsync();
48+
var notifications = await _db.Notification.Where(n => n.UserName.ToLower() == userName.ToLower()).ToListAsync();
4949
return notifications;
5050
}
5151

@@ -69,5 +69,16 @@ public async Task DeleteAllForPost(int postId)
6969
notifications.ForEach(n => _db.Remove(n));
7070
await _db.SaveChangesAsync();
7171
}
72+
73+
/// <summary>
74+
/// Delete all notifications for a specified user
75+
/// </summary>
76+
/// <param name="username"></param>
77+
public async Task DeleteAllForUser(string username)
78+
{
79+
var notifications = await _db.Notification.Where(n => n.UserName.ToLower() == username.ToLower()).ToListAsync();
80+
notifications.ForEach(n => _db.Remove(n));
81+
await _db.SaveChangesAsync();
82+
}
7283
}
7384
}

devblog/devblog/client/src/helpers.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ pub fn on_click(
4848
});
4949
}
5050

51+
pub fn clear_user_data(dispatch: Dispatch<Store>) {
52+
dispatch.reduce(|_| Store::default().into());
53+
}
54+
5155
pub fn set_user_data(dispatch: Dispatch<Store>, store: Store) {
5256
dispatch.reduce_mut(move |s| {
5357
s.token = store.token;
@@ -86,6 +90,7 @@ pub fn on_submit(
8690
if let Some(res) = api.fetch(Some(hdrs), body, Method::POST).await {
8791
if res.status() == 200 {
8892
let obj: Store = serde_json::from_str(&res.text().await.unwrap()).unwrap();
93+
log!("FIX ME");
8994
set_user_data(dispatch, obj);
9095
nav.push(&Route::Home);
9196
}

devblog/devblog/client/src/pages/sign_out.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ pub fn sign_out() -> Html {
1414
let nav = use_navigator().unwrap();
1515
let (_, dispatch) = use_store::<Store>();
1616

17+
let sign_out = {
18+
helpers::clear_user_data(dispatch.clone());
19+
helpers::on_submit(&user, nav, Rc::new(Api::SignOut), dispatch)
20+
};
21+
1722
html! {
18-
<form class={style} onsubmit={helpers::on_submit(&user, nav, Rc::new(Api::SignOut), dispatch)}>
23+
// <form class={style} onsubmit={helpers::on_submit(&user, nav, Rc::new(Api::SignOut), dispatch)}>
24+
<form class={style} onsubmit={sign_out}>
1925
<button class="logout">{"Logout"}</button>
2026
</form>
2127
}

0 commit comments

Comments
 (0)