예제 #1
0
        public IActionResult Liked([FromBody] Interaction interaction)
        {
            if (interaction.IsLike == null || interaction.LikedBy == null || interaction.ProductId == 0)
            {
                return(BadRequest(new Exception("Invalid Like")));
            }
            interaction.LikedDate = DateTime.UtcNow;
            var like = _context.Interaction.FirstOrDefault(x => x.LikedBy == interaction.LikedBy && x.ProductId == interaction.ProductId);

            if (like == null)
            {
                _context.Interaction.Add(interaction);
            }
            else
            {
                like.IsLike    = interaction.IsLike;
                like.LikedBy   = interaction.LikedBy;
                like.LikedDate = interaction.LikedDate;
                _context.Interaction.Update(like);
            }
            Product post = _context.Product.Include(x => x.Poster).FirstOrDefault(x => x.Id == interaction.ProductId);

            if (interaction.LikedBy != post.PosterId)
            {
                string likedBy            = _context.MUser.FirstOrDefault(x => x.Id == interaction.LikedBy).Name;
                string postOwner          = post.Poster.Name;
                string postName           = post.ProductName;
                string likeText           = interaction.IsLike == true ? "liked" : "unliked";
                NotificationRequest notif = new NotificationRequest()
                {
                    Body  = "Hi " + postOwner + ", " + likedBy + " " + likeText + " your post.",
                    Title = "Post " + likeText
                };

                Notification newNotif = new Notification(notif)
                {
                    To            = post.Poster.Id,
                    ModuleId      = interaction.ProductId,
                    ModuleName    = "product",
                    ModuleUseCase = "like",
                    PushDate      = DateTime.UtcNow
                };
                _context.Notification.Add(newNotif);

                if (post.Poster.PushId != null)
                {
                    NotificationRequestToTopic body = new NotificationRequestToTopic(notif, post.Poster.PushId);
                    string jsonBody = JsonConvert.SerializeObject(body);
                    PushNotificationService.sendPushNotification(jsonBody);
                }
            }
            _context.Commit();
            return(Ok());
        }
예제 #2
0
        public async Task <IActionResult> CheckUpdateStatus()
        {
            var interval = _context.SysParam.FirstOrDefault(x => x.ParamCode == "DEFAULT_PRODUCT_INTERVAL").ParamValue;
            var products = _context.Product
                           .Include(x => x.Poster)
                           .Where(x => x.IsActive && x.UpdatedDate.AddSeconds(int.Parse(interval)).CompareTo(DateTime.UtcNow) < 1)
                           .ToList();

            if (products == null)
            {
                return(Ok());
            }
            foreach (var item in products)
            {
                // var interval = int.Parse(item.UpdateIntervalInSecond);
                // var dateTimeProduct = item.UpdatedDate;
                // var dateTimeNow = DateTime.UtcNow;
                // dateTimeProduct.AddSeconds(interval);
                // var compare = dateTimeProduct.CompareTo(dateTimeNow);
                item.IsActive = false;
                _context.Product.Update(item);
                NotificationRequest notif = new NotificationRequest()
                {
                    Body  = item.ProductName + " is diactivated, please update your product to make it active again",
                    Title = "Update Product",
                };
                Notification newNotif = new Notification(notif)
                {
                    To            = item.Poster.Id,
                    ModuleId      = item.Id,
                    ModuleName    = "product",
                    ModuleUseCase = "expired",
                    PushDate      = DateTime.UtcNow
                };
                _context.Notification.Add(newNotif);
                if (item.Poster.PushId != null)
                {
                    NotificationRequestToTopic body = new NotificationRequestToTopic(notif, item.Poster.PushId);
                    body.data.notId = item.Id;
                    string jsonBody = JsonConvert.SerializeObject(body);
                    PushNotificationService.sendPushNotification(jsonBody);
                }
            }
            await _context.SaveChangesAsync();

            return(Ok(products.Count() + " product need to be updated"));
        }
        public IActionResult NotificationToTopics([FromBody] NotificationRequest notif, string topic)
        {
            var newNotif = new Notification(notif)
            {
                Topic    = topic,
                PushDate = DateTime.UtcNow
            };

            _context.Notification.Add(newNotif);
            _context.Commit();

            var toTopic = "/topics/" + topic;

            NotificationRequestToTopic body = new NotificationRequestToTopic(notif, toTopic);
            string jsonBody = JsonConvert.SerializeObject(body);

            return(Ok(PushNotificationService.sendPushNotification(jsonBody)));
        }
예제 #4
0
        public IActionResult ChangeAppointmentStatus([FromBody] AppointmentUpdateStatusRequest statusRequest, int appointmentId)
        {
            if (statusRequest.Status.ToLower().Contains("rejected") || statusRequest.Status.ToLower().Contains("approved") || statusRequest.Status.ToLower().Contains("done") || statusRequest.Status.ToLower().Contains("deleted"))
            {
                var appointment = _context.Appointment
                                  .Include(x => x.Maker)
                                  .Include(x => x.Recepient)
                                  .FirstOrDefault(x => x.Id == appointmentId);
                appointment.Status        = statusRequest.Status.ToUpper();
                appointment.RejectMessage = statusRequest.RejectMessage;
                _context.Appointment.Update(appointment);
                _context.Commit();

                var statusMessage = statusRequest.Status.ToLower();

                NotificationRequest notif = new NotificationRequest()
                {
                    Body  = "Hi " + appointment.Maker.Name + ", your meeting with " + appointment.Recepient.Name + " has been " + statusMessage,
                    Title = "Meeting " + statusMessage.First().ToString().ToUpper() + statusMessage.Substring(1)
                };

                Notification newNotif = new Notification(notif)
                {
                    To            = appointment.MakerId,
                    ModuleId      = appointment.Id,
                    ModuleName    = "appointment",
                    ModuleUseCase = "status",
                    PushDate      = DateTime.UtcNow
                };
                _context.Notification.Add(newNotif);
                if (appointment.Maker.PushId != null)
                {
                    NotificationRequestToTopic body = new NotificationRequestToTopic(notif, appointment.Maker.PushId);
                    string jsonBody = JsonConvert.SerializeObject(body);
                    PushNotificationService.sendPushNotification(jsonBody);
                }
                return(Ok());
            }
            else
            {
                return(NotFound());
            }
        }
예제 #5
0
        public void SendChatMessage(ChatMessagesRequest data)
        {
            List <int> listId = new List <int> ();

            listId.Add(data.SenderId);
            listId.Add(data.ReceiverId);

            List <MUser> listUser = _dbcontext.MUser.Where(x => listId.Contains(x.Id)).ToList();
            var          sender   = listUser.FirstOrDefault(x => x.Id == data.SenderId);
            var          receiver = listUser.FirstOrDefault(x => x.Id == data.ReceiverId);

            ChatMessages chatMessage = new ChatMessages()
            {
                Date       = DateTime.UtcNow,
                ReceiverId = receiver.Id,
                SenderId   = sender.Id,
                RoomId     = data.RoomId,
                Message    = data.Message,
                IsRead     = false,
            };

            _dbcontext.ChatMessages.Add(chatMessage);
            _dbcontext.Commit();

            if (receiver.SocketId == null && sender.PushId != null)
            {
                NotificationRequest body = new NotificationRequest()
                {
                    Body  = "New chat message from " + sender.Name,
                    Title = "New chat message",
                };

                NotificationRequestToTopic request = new NotificationRequestToTopic(body, receiver.PushId);
                request.data.newChat = "True";
                string jsonBody = JsonConvert.SerializeObject(request);
                PushNotificationService.sendPushNotification(jsonBody);
            }
            else if (receiver.SocketId != null)
            {
                Clients.Client(receiver.SocketId).SendAsync("send:chat", new { msg = "Chat Received", roomId = data.RoomId, chat = data.Message, dateTime = chatMessage.Date });
            }
        }
예제 #6
0
        public IActionResult Commented([FromBody] Interaction interaction)
        {
            if (interaction.IsComment == null || interaction.CommentBy == null || interaction.ProductId == 0)
            {
                return(BadRequest(new Exception("Invalid Comment")));
            }
            interaction.CommentDate = DateTime.UtcNow;
            _context.Interaction.Add(interaction);

            Product post = _context.Product.Include(x => x.Poster).FirstOrDefault(x => x.Id == interaction.ProductId);

            if (interaction.CommentBy != post.PosterId)
            {
                string commentBy          = _context.MUser.FirstOrDefault(x => x.Id == interaction.CommentBy).Name;
                string postOwner          = post.Poster.Name;
                string postName           = post.ProductName;
                NotificationRequest notif = new NotificationRequest()
                {
                    Body  = "Hi " + postOwner + ", " + commentBy + " commented your post.",
                    Title = "Post Commented"
                };

                Notification newNotif = new Notification(notif)
                {
                    To            = post.Poster.Id,
                    ModuleId      = interaction.ProductId,
                    ModuleName    = "product",
                    ModuleUseCase = "comment",
                    PushDate      = DateTime.UtcNow
                };
                _context.Notification.Add(newNotif);
                if (post.Poster.PushId != null)
                {
                    NotificationRequestToTopic body = new NotificationRequestToTopic(notif, post.Poster.PushId);
                    string jsonBody = JsonConvert.SerializeObject(body);
                    PushNotificationService.sendPushNotification(jsonBody);
                }
            }
            _context.Commit();
            return(Ok());
        }
예제 #7
0
        public IActionResult CreateAppointment([FromBody] AppointmentRequest request)
        {
            var appointment = new Appointment(request, "PENDING");

            _context.Appointment.Add(appointment);
            _context.Commit();

            var createdAppointment = _context.Appointment
                                     .Include(x => x.Maker)
                                     .Include(x => x.Recepient)
                                     .LastOrDefault(x => x.Date == request.Date && x.LocationCoords == request.LocationCoords && x.RecepientId == request.RecepientId);

            NotificationRequest notif = new NotificationRequest()
            {
                Body  = "Hi " + createdAppointment.Recepient.Name + ", " + createdAppointment.Maker.Name + " want to arrange meeting with you",
                Title = "Meeting Arranged"
            };

            Notification newNotif = new Notification(notif)
            {
                To            = createdAppointment.RecepientId,
                ModuleId      = createdAppointment.Id,
                ModuleName    = "appointment",
                ModuleUseCase = "create",
                PushDate      = DateTime.UtcNow
            };

            _context.Notification.Add(newNotif);
            if (createdAppointment.Recepient.PushId != null)
            {
                NotificationRequestToTopic body = new NotificationRequestToTopic(notif, createdAppointment.Recepient.PushId);
                string jsonBody = JsonConvert.SerializeObject(body);
                PushNotificationService.sendPushNotification(jsonBody);
            }
            _context.Commit();
            return(Ok());
        }