コード例 #1
0
ファイル: MoodService.cs プロジェクト: sharpflinger/ImageMood
        public ImageMoodViewModel AddMood(Mood mood, int imageId)
        {
            if (mood == null) throw new ArgumentNullException("mood");

            if (IsMoodExists(mood.Name, imageId))
            {
                throw new ArgumentException("Mood already exist for image");
            }

            var existingMood = _moodRepository.Find(x => x.Name.ToUpper() == mood.Name.ToUpper(), i => i.MoodToImages);
            var moodToAdd = existingMood ?? mood;

            var image = _imageRepository.Find(imageId);

            image.AddMood(moodToAdd, UserContext);

            _imageRepository.SaveChanges();

            var moodToImage = moodToAdd.MoodToImages.First(x => x.ImageId == imageId);

            return new ImageMoodViewModel
                {
                    Id = moodToImage.Id,
                    Mood = new MoodViewModel
                        {
                            Id = moodToImage.Mood.Id,
                            Name = moodToImage.Mood.Name
                        },
                        Rating = moodToImage.Rating,
                        Voted = true
                };
        }
コード例 #2
0
 public virtual ActionResult AddMood(Mood mood, int imageId)
 {
     var imageMoodViewModel = _moodsService.AddMood(mood, imageId);
     return View(imageMoodViewModel);
 }
コード例 #3
0
ファイル: Image.cs プロジェクト: sharpflinger/ImageMood
 public void AddMood(Mood mood, UserContext userContext)
 {
     if (mood.Id == 0)
     {
         mood.Create();
     }
     var moodToImage = new MoodToImage {Mood = mood};
     moodToImage.Vote(userContext.UserGuid);
     ImageMoods.Add(moodToImage);
 }