Ejemplo 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." }
                           ));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> IsSubscriptionActive(WebPushSubscriptionJSON subscriptionDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

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

            if (string.IsNullOrEmpty(sub.Endpoint))
            {
                return(StatusCode(StatusCodes.Status410Gone, new { message = "subscription was not found" }));
            }
            else
            {
                return(Ok());
            }
        }
Ejemplo n.º 3
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()));
        }