Пример #1
0
        public async Task <Response <TotalMessage> > GetGlobalMessage(int messageId, int repId)
        {
            try
            {
                var msg = await _repo.GetGlobalMessage(messageId);

                if (msg == null)
                {
                    return(new Response <TotalMessage>("Error :Message Is not found "));
                }
                var gMsg = new TotalMessage
                {
                    Content   = msg.MessageContent.Content,
                    Id        = msg.Id,
                    DateTime  = msg.DateTime,
                    TypesEnum = NotificationTypesEnum.GlobalMessage
                };
                var handler        = new GlobalMessageRead(msg, _repo, repId);
                var readingHandler = new GlobalMessageReadingHandler(_repo, repId);
                readingHandler.SetObj(new List <IReadable <int> > {
                    handler
                });
                var readGlobal = readingHandler.ReadAll();
                await _unitWork.CommitAsync();

                if (readGlobal.Any())
                {
                    gMsg.Read = false;
                }

                return(new Response <TotalMessage>(gMsg));
            }
            catch (Exception e)
            {
                return(new Response <TotalMessage>($"Error :{e.Message}"));
            }
        }
Пример #2
0
        public async Task <Response <IEnumerable <TotalMessage> > > GetAllMessages(int repId, int pageNumber, int pageSize)
        {
            try
            {
                var messages = _repo.GetMessages(repId, pageNumber, pageSize);
                var global   = _repo.GetGlobalMessages(pageNumber, pageSize);
                var all      = messages.Select(message => new TotalMessage
                {
                    Content   = message.Content.Content,
                    Id        = message.Id,
                    DateTime  = message.DateTime,
                    TypesEnum = (NotificationTypesEnum)message.Content.NotificationTypeId,
                })
                               .Union(global.Select(globals => new TotalMessage
                {
                    Content   = globals.MessageContent.Content,
                    Id        = globals.Id,
                    DateTime  = globals.DateTime,
                    TypesEnum = (NotificationTypesEnum)globals.MessageContent.NotificationTypeId,
                }))
                               .OrderBy(message => message.DateTime)
                               .Skip((pageNumber - 1) * pageSize).Take(pageSize)
                               .ToDictionary(message => new MessageKey
                {
                    Id   = message.Id,
                    Type = (int)message.TypesEnum
                });

                /**
                 * Handling Reading for both types
                 */
                var messageReading = new ReadingHandler <int>();
                messageReading.SetObj(new List <IReadable <int> >(messages
                                                                  .Select(message => new MessageRead(message))));
                var messageRead = messageReading.ReadAll();

                var globalHandler = new GlobalMessageReadingHandler(_repo, repId);
                globalHandler.SetObj(new List <IReadable <int> >(
                                         global.Select(message => new GlobalMessageRead(message, _repo, repId))));
                var globalRead = globalHandler.ReadAll();

                //end of reading
                foreach (var message in messageRead)
                {
                    all[new MessageKey {
                            Type = (int)NotificationTypesEnum.Message, Id = message
                        }].Read = false;
                }

                foreach (var message in globalRead)
                {
                    all[new  MessageKey {
                            Type = (int)NotificationTypesEnum.Message, Id = message
                        }].Read = false;
                }

                await _unitWork.CommitAsync();

                return(new Response <IEnumerable <TotalMessage> >(all.Values));
            }
            catch (Exception e)
            {
                return(new Response <IEnumerable <TotalMessage> >($"ERROR :{e.Message}"));
            }
        }