using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; using FirebaseAdmin; using FirebaseAdmin.Messaging; using Google.Apis.Auth.OAuth2; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using MyCore.Framework.Business; using MyCore.Interfaces.DTO; using MyCore.Interfaces.Models; using MyCore.Services; namespace MyCore.Service.Controllers { //[ApiController] [Authorize] [Route("[controller]")] public class NotificationController : ControllerBase { private readonly ILogger _logger; private HomeDatabaseService _HomeDatabaseService; private readonly FirebaseClient _firebaseClient; public NotificationController(ILogger logger, HomeDatabaseService homeDatabaseService, FirebaseClient firebaseClient) : base() { _logger = logger; this._HomeDatabaseService = homeDatabaseService; _firebaseClient = firebaseClient; } /// /// Create a fcm notification /// /// notificationDTO /// bool result [HttpPost("all")] [Consumes("application/json")] public async Task> CreateSimpleNotification([FromBody] NotificationDTO notificationDTO) { try { if (notificationDTO.isPushNotification) { if (notificationDTO.notificationTitle == null || notificationDTO.notificationMessage == null) return new BadRequestResult(); await NotificationLogic.PushFCMNotification(notificationDTO, null); return Ok(true); } return new BadRequestResult(); } catch (Exception ex) { _logger?.LogError(ex, $"Error sending global notification to all users"); return new BadRequestResult(); } } /// /// Create a fcm notification for a specific home /// /// homeId /// notificationDTO /// bool result [HttpPost("home/{homeId}")] [Consumes("application/json")] public async Task> CreateSimpleNotificationForSpecificUser(string homeId, [FromBody] NotificationDTO notificationDTO) { try { if (notificationDTO.isPushNotification) { if (notificationDTO.notificationTitle == null || notificationDTO.notificationMessage == null || homeId == null) return new BadRequestResult(); Home home = _HomeDatabaseService.GetById(homeId); await NotificationLogic.PushFCMNotification(notificationDTO, home); return Ok(true); } return new BadRequestResult(); } catch (Exception ex) { _logger?.LogError(ex, $"Error sending notification to specified home {homeId}"); return new BadRequestResult(); } } } }