Example #1
0
		public Post(TranslatedPost translatedPost, IEnumerable<string> cultures) : this()
		{
			Id = translatedPost.Id;
			Author = translatedPost.Author;
			foreach (var culture in cultures)
			{
				Translations.Add(new PostTranslation
				{
					Culture = culture,
					PostId = translatedPost.Id,
					Title = translatedPost.Title,
					Summary = translatedPost.Summary,
					SummaryImage = translatedPost.SummaryImage,
					ContentImage = translatedPost.ContentImage,
					Tags = translatedPost.Tags
				});
			}
		}
		// POST http://localhost:51102/odata/Posts
		// User-Agent: Fiddler
		// Host: localhost:14270
		// Content-type: application/json
		// Accept: application/json
		// Content-Length: 26
		// { id:"workmarketinnet-launched", title:"WorkMarketinNet launched", description:"Finally. After months of development WorkMarketingNet is alive." }
		//[Authorize(Roles = "Admin")]
		public async Task<IHttpActionResult> Post(TranslatedPost translatedPost, [ValueProvider(typeof(CultureValueProviderFactory))] string culture = "en-US")
		{
			translatedPost.Culture = culture;

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

			try
			{
				var post = new Post(translatedPost, _cultureManager.SupportedCultures);
				var newPost = _postsManager.Post(post);

				await _postsManager.SaveChanges();
				translatedPost.Id = newPost.Id;
				return Created(translatedPost);
			}
			catch (Exception ex)
			{
				throw;
			}
		}
		// PUT  http://localhost:51102/odata/Posts('workmarketinnet-launched')
		// User-Agent: Fiddler
		// Host: localhost:14270
		// Content-type: application/json
		// Accept: application/json
		// Content-Length: 34
		// { id:"workmarketinnet-launched", title:"WorkMarketinNet launched [PUT]", description:"Finally. After months of development WorkMarketingNet is alive." }
		//[Authorize(Roles = "Admin")]
		public async Task<IHttpActionResult> Put([FromODataUri] string key, TranslatedPost updatePost, [ValueProvider(typeof(CultureValueProviderFactory))] string culture = "en-US")
		{
			updatePost.Culture = culture;

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

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

			var post = new Post(updatePost, new[] { culture });
			var modelPost = _postsManager.Put(key, post);

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

		}
Example #4
0
		public Post(TranslatedPost translatedPost, string culture) : this(translatedPost, new[] { culture }) { }