public TranslatedComment(Comment comment)
		{
			Id = comment.Id;
			CommentableId = comment.CommentableId;
			Body = comment.Body;
			PublishDate = comment.PublishDate;
			Author = comment.Author;
			IsSpam = comment.IsSpam;
			//VotesUp = comment.VotesUp;
			//VotesDown = comment.VotesDown;
			//LikesCount = comment.VotesUp.Value - comment.VotesDown.Value;
			//ReplyToComment = comment.ReplyToComment != null ? new TranslatedComment(comment.ReplyToComment) : null;
			//Responses = comment.Responses?.Select(c => new TranslatedComment(c)).ToList();
			if (comment.Translations != null && comment.Translations.Any())
			{
				var translation = comment.Translations.First();
				Culture = translation.Culture;
				BodyTranslation = translation.Body;
			}
		}
Пример #2
0
		public void Patch(Comment patch)
		{
			if (patch.CommentableId != null) CommentableId = patch.CommentableId;
			if (patch.Body != null) Body = patch.Body;
			if (patch.PublishDate != null) PublishDate = patch.PublishDate;
			if (patch.Author?.Name != null) Author.Name = patch.Author.Name;
			if (patch.Author?.Url != null) Author.Name = patch.Author.Url;
			if (patch.Author?.Photo != null) Author.Name = patch.Author.Photo;
			if (patch.IsSpam != null) IsSpam = patch.IsSpam;
			//if (patch.VotesUp != null) VotesUp = patch.VotesUp;
			//if (patch.VotesDown != null) VotesDown = patch.VotesDown;
			// ReplyToComment ?
			// Responses ?
			foreach (var translationPatch in patch.Translations)
			{
				var translation = Translations.FirstOrDefault(t => t.Culture == translationPatch.Culture);
				if (translation == null) { continue; }

				if (translationPatch.Body != null) translation.Body = translationPatch.Body;
			}
		}
		// POST http://localhost:51099/odata/Comments
		// User-Agent: Fiddler
		// Host: localhost:14270
		// Content-type: application/json
		// Accept: application/json
		// Content-Length: 26
		// { id:"comment-1", title:"Comment 1", description:"This is comment 1." }
		//[Authorize(Roles = "Admin")]
		public async Task<IHttpActionResult> Post(TranslatedComment translatedComment, [ValueProvider(typeof(CultureValueProviderFactory))] string culture = "en-US")
		{
			translatedComment.Culture = culture;

			if (!ModelState.IsValid)
			{
				return BadRequest(ModelState);
			}

			try
			{
				var comment = new Comment(translatedComment, _cultureManager.SupportedCultures);
				var newComment = _commentsManager.Post(comment);

				await _commentsManager.SaveChanges();
				translatedComment.Id = newComment.Id;
				return Created(translatedComment);
			}
			catch (Exception ex)
			{
				throw;
			}
		}
		// PUT  http://localhost:51099/odata/Comments('comment-1')
		// User-Agent: Fiddler
		// Host: localhost:14270
		// Content-type: application/json
		// Accept: application/json
		// Content-Length: 34
		// { id:"comment-1", title:"Comment 1 [PUT]", description:"This is comment 1." }
		//[Authorize(Roles = "Admin")]
		public async Task<IHttpActionResult> Put([FromODataUri] Guid key, TranslatedComment updateComment, [ValueProvider(typeof(CultureValueProviderFactory))] string culture = "en-US")
		{
			updateComment.Culture = culture;

			if (!ModelState.IsValid)
			{
				return BadRequest(ModelState);
			}

			if (key != updateComment.Id)
			{
				return BadRequest();
			}

			var comment = new Comment(updateComment, new[] { culture });
			var modelComment = _commentsManager.Put(key, comment);

			try
			{
				await _commentsManager.SaveChanges();
			}
			catch (DbUpdateConcurrencyException)
			{
				if (!_commentsManager.Exists(modelComment.Id))
				{
					return NotFound();
				}
				else
				{
					throw;
				}
			}
			return Updated(modelComment);

		}
		public async Task<IHttpActionResult> Patch([FromODataUri] Guid key, Delta<TranslatedComment> delta, [ValueProvider(typeof(CultureValueProviderFactory))] string culture = "en-US")
		{
			var comment = new Comment(delta.GetEntity(), culture);
			var tochange = _commentsManager.Patch(key, comment);

			try
			{
				await _commentsManager.SaveChanges();
			}
			catch (DbUpdateConcurrencyException)
			{
				if (!_commentsManager.Exists(tochange.Id))
				{
					return NotFound();
				}
				else
				{
					throw;
				}
			}

			return Updated(tochange);

		}