public async Task <ActionResult> Message(int messageId, string messageText) { var userIdCurrent = User.Identity.GetUserId(); var lastMessage = _messageService.Find(messageId); string messageTo; if (userIdCurrent == lastMessage.FromUserId) { messageTo = lastMessage.ToUserId; } else { messageTo = lastMessage.FromUserId; } var message = new Message() { FromUserId = userIdCurrent, Content = messageText, ObjectState = ObjectState.Added, AdvertisementId = lastMessage.AdvertisementId, ToUserId = messageTo }; _messageService.Insert(message); var messageNotification = new MessageNotification() { Message = message, DateTime = DateTime.Now, ObjectState = ObjectState.Added }; var userNotification = new UserNotification() { IsRead = false, Notification = messageNotification, UserId = message.ToUserId, ObjectState = ObjectState.Added }; _userNotificationService.Insert(userNotification); await _unitOfWorkAsync.SaveChangesAsync(); TempData[TempKeys.UserMessage] = "Wiadomość została wysłana"; return(RedirectToAction("Message", new { advertisementId = lastMessage.AdvertisementId })); }
public async Task <ActionResult> Create(AdvertisementViewModel viewModel) { if (!ModelState.IsValid || viewModel.PhotoFiles == null || viewModel.PhotoFiles.First() == null) { viewModel.Manufacturers = _sqlDbService.GetAllManufactures().ToList(); if (viewModel.PhotoFiles == null || viewModel.PhotoFiles.First() == null) { TempData[TempKeys.UserMessageAlertState] = "bg-danger"; TempData[TempKeys.UserMessage] = "Zdjęcia w ogłoszeniu są wymagane!"; } return(View("Form", viewModel)); } var userId = User.Identity.GetUserId(); var car = new Car() { EngineCap = viewModel.EngineCap, FuelType = viewModel.FuelType, Mileage = viewModel.Mileage, ModelId = viewModel.Model, Price = viewModel.Price, Year = viewModel.Year, ObjectState = ObjectState.Added }; var selectedFeatureIds = viewModel.Features.Where(x => x.IsChecked).Select(x => x.Id).ToList(); foreach (var selectedFeatureId in selectedFeatureIds) { car.Features.Add(new Feature() { Id = selectedFeatureId }); } List <Photo> photos = new List <Photo>(); foreach (var photoFile in viewModel.PhotoFiles) { Photo photo = new Photo(); using (var reader = new System.IO.BinaryReader(photoFile.InputStream)) { photo.Content = reader.ReadBytes(photoFile.ContentLength); } photo.Extension = photoFile.ContentType; photo.ObjectState = ObjectState.Added; photos.Add(photo); } var advertisement = new Advertisement() { AddedDate = DateTime.Now, Description = viewModel.Description, Car = car, IsActive = true, Title = viewModel.Title, UserId = userId, Photos = photos, ObjectState = ObjectState.Added }; _advertisementService.InsertOrUpdateGraph(advertisement); var followers = _followingService.GetFollowingByFollowee(userId).Select(x => x.FollowerId).ToList(); if (followers.Any()) { var followingNotification = new FollowingNotification() { Advertisement = advertisement, DateTime = DateTime.Now, ObjectState = ObjectState.Added }; foreach (var followeer in followers) { var userNotification = new UserNotification() { IsRead = false, Notification = followingNotification, UserId = followeer, ObjectState = ObjectState.Added }; _userNotificationService.Insert(userNotification); } } await _unitOfWorkAsync.SaveChangesAsync(); return(RedirectToAction("Details", "Advertisement", new { id = advertisement.Id })); }