Exemplo n.º 1
0
        public async Task <IActionResult> RegisterNewSubscription(WebPushSubscriptionJSON subscriptionDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var subscription = new WebPushSubscription
            {
                Endpoint       = subscriptionDTO.Endpoint,
                ExpirationTime = subscriptionDTO.ExpirationTime ?? -1,
                P256DH         = subscriptionDTO.Keys.P256dh,
                Auth           = subscriptionDTO.Keys.Auth
            };

            using var dbContext = _db.CreateDbContext();
            dbContext.Subscriptions.Add(subscription);

            try
            {
                return(Ok(await dbContext.SaveChangesAsync()));
            }
            catch (DbUpdateException)
            {
                return(StatusCode(
                           StatusCodes.Status500InternalServerError,
                           new { message = "Error while creating new temperature reading." }
                           ));
            }
        }
Exemplo n.º 2
0
        public IActionResult Subscribe([FromBody] WebPushModel subscriptionModel)
        {
            var newSubscription = new WebPushSubscription
            {
                Auth     = subscriptionModel.Auth,
                EndPoint = subscriptionModel.EndPoint,
                P256dH   = subscriptionModel.P256dH
            };

            _context.WebPushSubscriptions.Add(newSubscription);
            _context.SaveChanges();
            return(Ok(newSubscription));
        }
Exemplo n.º 3
0
        public async Task <bool> AddNotificationBrowser(int userId, WebPushSubscription subscription)
        {
            using (var db = _dbService.GetConnection())
            {
                var existingNotifications = (await GetCurrentSubscriptions(userId, db)).AsList();
                if (!existingNotifications.Any(n => n.Endpoint == subscription.Endpoint))
                {
                    existingNotifications.Add(subscription);
                }
                else
                {
                    return(true);
                }

                return((await SetCurrentSubscriptions(userId, existingNotifications, db)) == 1);
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> DeleteSubscription(WebPushSubscriptionJSON subscriptionDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            using var dbContext = _db.CreateDbContext();
            WebPushSubscription sub = await dbContext.Subscriptions
                                      .Where(s => s.Endpoint == subscriptionDTO.Endpoint)
                                      .FirstOrDefaultAsync();

            if (sub == null)
            {
                return(BadRequest("A subscription with the specified Endpoint does not exist."));
            }

            dbContext.Remove(sub);

            return(Ok(await dbContext.SaveChangesAsync()));
        }
Exemplo n.º 5
0
 public static WebPushSubscriptionDto FromDomainObject(WebPushSubscription source)
 {
     return(SimpleMapper.Map(source, new WebPushSubscriptionDto()));
 }