Пример #1
0
        public ActionResult <AdminsPostDTO> AddNewAdminsPost(AdminsPostDTO post)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            adminsPostService.AddNewPost(post);
            pushNotificationsService.SendPostPushNotification(post);
            return(StatusCode(StatusCodes.Status201Created, post));
        }
Пример #2
0
 public void AddNewPost(AdminsPostDTO post)
 {
     adminsPostRepository.Add(mapper.Map <AdminsPostDTO, AdminsPost>(post));
     theaterScheduleUnitOfWork.Save();
 }
Пример #3
0
        public void SendPostPushNotification(AdminsPostDTO post)
        {
            if (post.IsPersonal)
            {
                int userId = post.ToUserId.GetValueOrDefault();
                IEnumerable <PushTokenDataModelPartial> pushTokenById = pushTokenRepository.GetPushTokenByAccountId(userId);
                var pushNotificationById =
                    (from partialInfo in pushTokenById
                     select new PushTokenDataModel
                {
                    Token = partialInfo.Token,
                    LanguageCode = partialInfo.LanguageCode,
                }).ToList();
                List <PushNotificationDTO> reqBody = (pushNotificationById.Select(p =>
                                                                                  new PushNotificationDTO
                {
                    To = p.Token,
                    Title = p.LanguageCode == "en" ? "New message" : "Нове повідомлення",
                    Body = p.LanguageCode == "en" ?
                           "You got new private message" : "Вам прийшло нове приватне повідомлення",
                    Color = "#9984d4",
                    Icon = "../img/logo.png"
                })).ToList();
                using (System.Net.WebClient client = new System.Net.WebClient())
                {
                    client.Headers.Add("accept", "application/json");
                    client.Headers.Add("accept-encoding", "gzip, deflate");
                    client.Headers.Add("Content-Type", "application/json");
                    client.UploadString(
                        "https://exp.host/--/api/v2/push/send",
                        JsonConvert.SerializeObject(reqBody));
                }
            }



            else
            {
                IEnumerable <PushTokenDataModelPartial> allPushTokens = pushTokenRepository.GetAllPushTokensWithoutPerformances();
                var allTokens = (from pushToken in allPushTokens
                                 select new PushTokenDataModel
                {
                    Token = pushToken.Token,
                    LanguageCode = pushToken.LanguageCode,
                }).ToList();
                List <PushNotificationDTO> reqBody = (allTokens.Select(p =>
                                                                       new PushNotificationDTO
                {
                    To = p.Token,
                    Title = p.LanguageCode == "en" ? "New message" : "Нове повідомлення",
                    Body = p.LanguageCode == "en" ?
                           "You got new public message" : "Вам прийшло нове спільне повідомлення",
                    Color = "#9984d4",
                    Icon = "../img/logo.png"
                })).ToList();
                using (System.Net.WebClient client = new System.Net.WebClient())
                {
                    client.Headers.Add("accept", "application/json");
                    client.Headers.Add("accept-encoding", "gzip, deflate");
                    client.Headers.Add("Content-Type", "application/json");
                    client.UploadString(
                        "https://exp.host/--/api/v2/push/send",
                        JsonConvert.SerializeObject(reqBody));
                }
            }
        }
        static AdminsPostControllerTest()
        {
            publicPosts = new List <AdminsPostDTO>
            {
                new AdminsPostDTO()
                {
                    AdminsPostId = 0,
                    Subject      = "Subject",
                    PostText     = "Post Text",
                    PostDate     = DateTime.Now,
                    IsPersonal   = false,
                    ToUserId     = null
                },
                new AdminsPostDTO()
                {
                    AdminsPostId = 1,
                    Subject      = "Subject",
                    PostText     = "Post Text",
                    PostDate     = DateTime.Now,
                    IsPersonal   = false,
                    ToUserId     = null
                }
            };

            privatePosts = new List <AdminsPostDTO>
            {
                new AdminsPostDTO()
                {
                    AdminsPostId = 2,
                    Subject      = "Subject",
                    PostText     = "Post Text",
                    PostDate     = DateTime.Now,
                    IsPersonal   = true,
                    ToUserId     = 0
                },
                new AdminsPostDTO()
                {
                    AdminsPostId = 3,
                    Subject      = "Subject",
                    PostText     = "Post Text",
                    PostDate     = DateTime.Now,
                    IsPersonal   = true,
                    ToUserId     = 1
                },
                new AdminsPostDTO()
                {
                    AdminsPostId = 4,
                    Subject      = "Subject",
                    PostText     = "Post Text",
                    PostDate     = DateTime.Now,
                    IsPersonal   = true,
                    ToUserId     = 0
                },
                new AdminsPostDTO()
                {
                    AdminsPostId = 5,
                    Subject      = "Subject",
                    PostText     = "Post Text",
                    PostDate     = DateTime.Now,
                    IsPersonal   = true,
                    ToUserId     = 1
                }
            };

            newPost = new AdminsPostDTO()
            {
                AdminsPostId = 0,
                Subject      = "Subject",
                PostText     = "Post Text",
                PostDate     = DateTime.Now,
                IsPersonal   = false,
                ToUserId     = null
            };
        }