/
aprogrammer
/
blazor-workshop
Обзор
Документация
Войти
/
aprogrammer
/
blazor-workshop
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
save-points/07-javascript-interop/BlazingPizza.Server/NotificationsController.cs
43 строки
1 KB
Ed Charbeneau
Nullable (#365)
29 ноя 2023, 23:47
Не верифицирован
29 ноя 2023, 23:47
bb345a1
Код
Авторство
О чём код?
using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace BlazingPizza.Server { [Route("notifications")] [ApiController] [Authorize] public class NotificationsController : Controller { private readonly PizzaStoreContext _db; public NotificationsController(PizzaStoreContext db) { _db = db; } [HttpPut("subscribe")] public async Task<NotificationSubscription> Subscribe(NotificationSubscription subscription) { // We're storing at most one subscription per user, so delete old ones. // Alternatively, you could let the user register multiple subscriptions from different browsers/devices. var userId = PizzaApiExtensions.GetUserId(HttpContext); if (userId is null) { throw new UnauthorizedAccessException(); } var oldSubscriptions = _db.NotificationSubscriptions.Where(e => e.UserId == userId); _db.NotificationSubscriptions.RemoveRange(oldSubscriptions); // Store new subscription subscription.UserId = userId; _db.NotificationSubscriptions.Attach(subscription); await _db.SaveChangesAsync(); return subscription; } } }