public virtual async Task <IActionResult> Send([FromBody] Notification notification) { // TODO FTN: Add validation! var loggedInUserId = long.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var hasValidTopic = notification != null && notification.Topic == "news"; if (!hasValidTopic) { return(StatusCode(StatusCodes.Status400BadRequest, notification)); } try { var result = await _notificationService.Send(notification); return(Ok()); } catch (Exception) { _logger.LogError(LoggingEvents.INSERT_ITEM, $"Send({notification}) FAILED.", notification); return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public virtual async Task <IActionResult> AddTicketPurchase([FromBody] TicketPurchase ticketPurchase) { // TODO FTN: Add validation! var loggedInUserId = long.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var buyerUsername = User.FindFirst(ClaimTypes.Name).Value; var buyerFirstName = User.FindFirst(ClaimTypes.GivenName).Value; string deviceId = null; bool enableNotifications = true; var hasTypeAndUser = ticketPurchase != null && ticketPurchase.TypeId != null && ticketPurchase.TypeId > 0 && ticketPurchase.UserId != null && ticketPurchase.UserId > 0 && ticketPurchase.NumberOfPassangers > 0; if (!hasTypeAndUser) { return(StatusCode(StatusCodes.Status400BadRequest, ticketPurchase)); } if (loggedInUserId != ticketPurchase.UserId) { return(StatusCode(StatusCodes.Status400BadRequest, ticketPurchase)); } try { if (_context.Purchases.FirstOrDefault(p => p.Id == ticketPurchase.Id) != null) { return(StatusCode(StatusCodes.Status409Conflict, ticketPurchase)); // 409 already exists! } deviceId = Request.Headers["DeviceID"]; enableNotifications = Request.Headers["Notifications"].Any() ? Request.Headers["Notifications"] != "false" : true; var type = _context.Types.First(t => t.Id == ticketPurchase.TypeId); var user = _context.Users.First(u => u.Id == ticketPurchase.UserId); if (user.Balance - type.Price * ticketPurchase.NumberOfPassangers < 0.0d) { return(StatusCode(StatusCodes.Status402PaymentRequired, ticketPurchase)); } ticketPurchase.Code = Guid.NewGuid(); ticketPurchase.StartDateTime = DateTime.Now.ToUniversalTime().AddMinutes(_configuration.GetSection(Startup.AppSettingsConfigurationSectionKey).GetValue <int>(Startup.AppSettingsMinutesUntilTicketStartKey)); ticketPurchase.EndDateTime = DateTime.Now.ToUniversalTime().AddMinutes(type.Duration.Value * 60 + _configuration.GetSection(Startup.AppSettingsConfigurationSectionKey).GetValue <int>(Startup.AppSettingsMinutesUntilTicketStartKey)); ticketPurchase.Price = type.Price; user.Balance = user.Balance - type.Price * ticketPurchase.NumberOfPassangers; _context.Purchases.Add(ticketPurchase); _context.SaveChanges(); ticketPurchase = _context.Purchases.Include(u => u.User).First(p => p.Id == ticketPurchase.Id); } catch (Exception) { _logger.LogError(LoggingEvents.INSERT_ITEM, "AddTicketPurchase({ticketPurchase}) NOT ADDED", ticketPurchase); return(StatusCode(StatusCodes.Status500InternalServerError)); } string notificationSent = "[ALL: False1 ; Device ID: False2]"; if (_newBuyerIsBIGNews) { var notification = new Notification() { Title = "We have a new ticket buyer!", Message = $"User: {buyerUsername} have bought a ticket of type id: '{ticketPurchase.TypeId}'! YEEEAAAH!", Topic = "news" }; var result = await _notificationService.Send(notification); notificationSent = notificationSent.Replace("False1", result.ToString()); } if (!string.IsNullOrEmpty(deviceId) && enableNotifications) { var notification = new Notification() { Title = "Your have bought a ticket. Congrats!", Message = $"Hello {buyerFirstName}. Your ticket '{ticketPurchase.Code}' is ready!", DeviceID = deviceId }; var result = await _notificationService.Send(notification); notificationSent = notificationSent.Replace("False2", result.ToString()); } Response.Headers.Add("DeviceID", deviceId); Response.Headers.Add("Notifications", enableNotifications.ToString()); Response.Headers.Add("NotificationSent", notificationSent.ToString()); return(StatusCode(StatusCodes.Status201Created, ticketPurchase)); }