Exemplo n.º 1
0
        public async Task <ControllerModels.Messages.AddThoughtResult> AddThought(int messageId, int smileyId)
        {
            var result = new ControllerModels.Messages.AddThoughtResult();

            var messageRecord = DbContext.Messages.Find(messageId);

            if (messageRecord is null || messageRecord.Deleted)
            {
                result.Errors.Add(string.Empty, $@"No message was found with the id '{messageId}'");
            }

            var smileyRecord = await DbContext.Smileys.FindAsync(smileyId);

            if (messageRecord is null)
            {
                result.Errors.Add(string.Empty, $@"No smiley was found with the id '{smileyId}'");
            }

            if (result.Errors.Any())
            {
                return(result);
            }

            var existingRecord = await DbContext.MessageThoughts
                                 .FirstOrDefaultAsync(mt =>
                                                      mt.MessageId == messageRecord.Id &&
                                                      mt.SmileyId == smileyRecord.Id &&
                                                      mt.UserId == CurrentUser.Id);

            if (existingRecord is null)
            {
                var messageThought = new DataModels.MessageThought {
                    MessageId = messageRecord.Id,
                    SmileyId  = smileyRecord.Id,
                    UserId    = CurrentUser.Id
                };

                DbContext.MessageThoughts.Add(messageThought);

                if (messageRecord.PostedById != CurrentUser.Id)
                {
                    var notification = new DataModels.Notification {
                        MessageId    = messageRecord.Id,
                        UserId       = messageRecord.PostedById,
                        TargetUserId = CurrentUser.Id,
                        Time         = DateTime.Now,
                        Type         = ENotificationType.Thought,
                        Unread       = true,
                    };

                    DbContext.Notifications.Add(notification);
                }
            }
            else
            {
                DbContext.Remove(existingRecord);

                var notification = await DbContext.Notifications.FirstOrDefaultAsync(item => item.MessageId == existingRecord.MessageId && item.TargetUserId == existingRecord.UserId && item.Type == ENotificationType.Thought);

                if (notification != null)
                {
                    DbContext.Remove(notification);
                }
            }

            await DbContext.SaveChangesAsync();

            result.TopicId   = messageRecord.TopicId;
            result.MessageId = messageRecord.Id;

            return(result);
        }
Exemplo n.º 2
0
        public async Task <ServiceModels.ServiceResponse> AddThought(InputModels.ThoughtInput input)
        {
            var serviceResponse = new ServiceModels.ServiceResponse();

            var messageRecord = DbContext.Messages.Find(input.MessageId);

            if (messageRecord is null)
            {
                serviceResponse.Error($@"No message was found with the id '{input.MessageId}'");
            }

            var smileyRecord = await DbContext.Smileys.FindAsync(input.SmileyId);

            if (messageRecord is null)
            {
                serviceResponse.Error($@"No smiley was found with the id '{input.SmileyId}'");
            }

            if (!serviceResponse.Success)
            {
                return(serviceResponse);
            }

            var existingRecord = await DbContext.MessageThoughts
                                 .SingleOrDefaultAsync(mt =>
                                                       mt.MessageId == messageRecord.Id &&
                                                       mt.SmileyId == smileyRecord.Id &&
                                                       mt.UserId == UserContext.ApplicationUser.Id);

            if (existingRecord is null)
            {
                var messageThought = new DataModels.MessageThought {
                    MessageId = messageRecord.Id,
                    SmileyId  = smileyRecord.Id,
                    UserId    = UserContext.ApplicationUser.Id
                };

                DbContext.MessageThoughts.Add(messageThought);

                if (messageRecord.PostedById != UserContext.ApplicationUser.Id)
                {
                    var notification = new DataModels.Notification {
                        MessageId    = messageRecord.Id,
                        UserId       = messageRecord.PostedById,
                        TargetUserId = UserContext.ApplicationUser.Id,
                        Time         = DateTime.Now,
                        Type         = Enums.ENotificationType.Thought,
                        Unread       = true,
                    };

                    DbContext.Notifications.Add(notification);
                }
            }
            else
            {
                DbContext.Remove(existingRecord);

                var notification = DbContext.Notifications.FirstOrDefault(item => item.MessageId == existingRecord.MessageId && item.TargetUserId == existingRecord.UserId && item.Type == Enums.ENotificationType.Thought);

                if (notification != null)
                {
                    DbContext.Remove(notification);
                }
            }

            DbContext.SaveChanges();

            serviceResponse.RedirectPath = UrlHelper.DirectMessage(input.MessageId);
            return(serviceResponse);
        }
Exemplo n.º 3
0
		public async Task<ServiceModels.ServiceResponse> AddThought(int messageId, int smileyId) {
			var serviceResponse = new ServiceModels.ServiceResponse();

			var messageRecord = DbContext.Messages.Find(messageId);

			if (messageRecord is null) {
				serviceResponse.Error($@"No message was found with the id '{messageId}'");
			}

			var smileyRecord = await DbContext.Smileys.FindAsync(smileyId);

			if (messageRecord is null) {
				serviceResponse.Error($@"No smiley was found with the id '{smileyId}'");
			}

			if (!serviceResponse.Success) {
				return serviceResponse;
			}

			var existingRecord = await DbContext.MessageThoughts
				.FirstOrDefaultAsync(mt =>
					mt.MessageId == messageRecord.Id
					&& mt.SmileyId == smileyRecord.Id
					&& mt.UserId == UserContext.ApplicationUser.Id);

			if (existingRecord is null) {
				var messageThought = new DataModels.MessageThought {
					MessageId = messageRecord.Id,
					SmileyId = smileyRecord.Id,
					UserId = UserContext.ApplicationUser.Id
				};

				DbContext.MessageThoughts.Add(messageThought);

				if (messageRecord.PostedById != UserContext.ApplicationUser.Id) {
					var notification = new DataModels.Notification {
						MessageId = messageRecord.Id,
						UserId = messageRecord.PostedById,
						TargetUserId = UserContext.ApplicationUser.Id,
						Time = DateTime.Now,
						Type = ENotificationType.Thought,
						Unread = true,
					};

					DbContext.Notifications.Add(notification);
				}
			}
			else {
				DbContext.Remove(existingRecord);

				var notification = DbContext.Notifications.FirstOrDefault(item => item.MessageId == existingRecord.MessageId && item.TargetUserId == existingRecord.UserId && item.Type == ENotificationType.Thought);

				if (notification != null) {
					DbContext.Remove(notification);
				}
			}

			DbContext.SaveChanges();

			await ForumHub.Clients.All.SendAsync("updated-message", new HubModels.Message {
				TopicId = messageRecord.ParentId > 0 ? messageRecord.ParentId : messageRecord.Id,
				MessageId = messageRecord.Id
			});

			serviceResponse.RedirectPath = UrlHelper.DisplayMessage(messageId);
			return serviceResponse;
		}