Example #1
0
        public IHttpActionResult Follow(FollowingDto followingDto)
        {
            var userId = User.Identity.GetUserId();

            if (_dbContext.Followings.Any(f => f.FollowerId == userId && f.FolloweeId == followingDto.FolloweeId))
            {
                return(BadRequest("Following already exitst!"));
            }

            var following = new Following
            {
                FollowerId = userId,
                FolloweeId = followingDto.FolloweeId
            };

            _dbContext.Followings.Add(following);
            _dbContext.SaveChanges();

            following = _dbContext.Followings
                        .Where(x => x.FolloweeId == followingDto.FolloweeId && x.FollowerId == userId)
                        .Include(x => x.Followee)
                        .Include(x => x.Follower).SingleOrDefault();

            var followingNotification = new FollowingNotification()
            {
                Id     = 0,
                Logger = following.Follower.Name + " following " + following.Followee.Name
            };

            _dbContext.FollowingNotifications.Add(followingNotification);
            _dbContext.SaveChanges();

            return(Ok());
        }
Example #2
0
        public IHttpActionResult UnFollow(string followeeId, string followerId)
        {
            var follow = _dbContext.Followings
                         .Where(x => x.FolloweeId == followeeId && x.FollowerId == followerId)
                         .Include(x => x.Followee)
                         .Include(x => x.Follower).SingleOrDefault();

            var followingNotification = new FollowingNotification()
            {
                Id     = 0,
                Logger = follow.Follower.Name + " unfollow " + follow.Followee.Name
            };

            _dbContext.FollowingNotifications.Add(followingNotification);

            _dbContext.Followings.Remove(follow);
            _dbContext.SaveChanges();
            return(Ok());
        }
        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 }));
        }