Example #1
0
		public Comment(TranslatedComment translatedComment, IEnumerable<string> cultures) : this()
		{
			Id = translatedComment.Id;
			CommentableId = translatedComment.CommentableId;
			Body = translatedComment.Body;
			PublishDate = translatedComment.PublishDate;
			Author = translatedComment.Author;
			IsSpam = translatedComment.IsSpam;
			//VotesUp = translatedComment.VotesUp;
			//VotesDown = translatedComment.VotesDown;
			//ReplyToComment = translatedComment.ReplyToComment != null ? new Comment(translatedComment.ReplyToComment, cultures) : null;
			//Responses = translatedComment.Responses?.Select(tc => new Comment(tc, cultures)).ToList();
			foreach (var culture in cultures)
			{
				Translations.Add(new CommentTranslation
				{
					Culture = culture,
					CommentId = translatedComment.Id,
					Body = translatedComment.BodyTranslation
				});
			}
		}
		// 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);

		}
Example #4
0
		public Comment(TranslatedComment translatedComment, string culture) : this(translatedComment, new[] { culture }) { }