public async Task <ViewServiceModel> PostAsync(CreateServiceModel comment) { var commentForDb = new Comment { Text = comment.Text, AdvertisementId = comment.AdvertisementId, UserId = comment.UserId, WrittenOn = comment.WrittenOn }; await context.Comments.AddAsync(commentForDb); await context.SaveChangesAsync(); var viewModel = new ViewServiceModel { Text = commentForDb.Text, Id = commentForDb.Id, UserId = commentForDb.UserId, WrittenOn = commentForDb.WrittenOn.ToString(GlobalConstants.DateTimeFormat), Username = userManager.FindByIdAsync(commentForDb.UserId).GetAwaiter().GetResult().UserName }; return(viewModel); }
private async Task NotifyAsync(ViewServiceModel comment, AdvertisementViewServiceModel ad) { var commentOwner = await userManager.FindByIdAsync(comment.UserId); var commentUserName = commentOwner.UserName; var adOwner = await userManager.FindByIdAsync(ad.UserId); var notificationActionLink = $"/Advertisement/Details?id={ad.Id}"; if (adOwner.Id != commentOwner.Id) { var notificationText = $"{commentUserName} commented on your Ad: '{comment.Text}'"; var notification = await notificationService.CreateNotificationAsync(notificationText, notificationActionLink); await notificationService.AssignNotificationToUserAsync(notification.Id, adOwner.Id); } var userIds = await userAdWishlistService.GetAllUserIdsThatHaveAdInWishlistAsync(ad.Id); var usersToGetNotification = userIds.Where(u => u != comment.UserId).ToList(); if (usersToGetNotification.Count != 0) { var notificationText = $"{commentUserName} commented on an Ad in your Wishlist: '{comment.Text}'"; var notification = await notificationService.CreateNotificationAsync(notificationText, notificationActionLink); await notificationService.AssignNotificationToUsersAsync(notification.Id, usersToGetNotification); } }
public async Task <ViewServiceModel> PostAsync(CreateServiceModel comment) { var commentForDb = new Comment { Text = comment.Text, ProductId = comment.ProductId, UserId = comment.UserId, WrittenOn = comment.WrittenOn, }; await this.commentRepository.AddAsync(commentForDb); await this.commentRepository.SaveChangesAsync(); var viewModel = new ViewServiceModel { Text = commentForDb.Text, Id = commentForDb.Id, UserId = commentForDb.UserId, WrittenOn = commentForDb.WrittenOn.ToString(GlobalConstants.DateTimeFormat), Username = this.userManager.FindByIdAsync(commentForDb.UserId).GetAwaiter().GetResult().UserName, }; return(viewModel); }