public Rating AddRating(int artistId, int fanId, int score) { using (var unitOfWork = new UnitOfWork()) { RatingValidator ratingValidator = new RatingValidator(); var checkResult = ratingValidator.Check(score); if (checkResult.Count != 0) { throw new InvalidModelException(String.Join("\n", checkResult.ToArray())); } var ratingRepository = unitOfWork.GetRepository <Rating>(); Rating foundRating = ratingRepository.GetAll().Where(r => r.ArtistId == artistId && r.FanId == fanId).FirstOrDefault(); Rating newRating = null; if (foundRating != null) { foundRating.Score = score; foundRating.Date = DateTime.Now; ratingRepository.Update(foundRating); newRating = foundRating; FileOperations.SaveRatingDataToFile(); } else if (foundRating == null) { newRating = ratingRepository.Add(new Rating { ArtistId = artistId, FanId = fanId, Score = score, Date = DateTime.Now }); FileOperations.AppendNewRatingLineToFile(fanId, artistId, score); } unitOfWork.Save(); return(newRating); } }