Exemple #1
0
		public void Patch(Photo patch)
		{
			foreach (var translationPatch in patch.Translations)
			{
				var translation = Translations.FirstOrDefault(t => t.Culture == translationPatch.Culture);
				if (translation == null) { continue; }

				if (translationPatch.Title != null) translation.Title = translationPatch.Title;
				if (translationPatch.Description != null) translation.Description = translationPatch.Description;
			}
		}
		// POST http://localhost:50481/odata/Photos
		// User-Agent: Fiddler
		// Host: localhost:14270
		// Content-type: application/json
		// Accept: application/json
		// Content-Length: 26
		// { id:"go-pro-7-steps", title:"Go Pro: 7 Steps to Becoming a Network Marketing Professional", description:"Over twenty years ago at a company convention, Eric Worre had an aha moment that changed his life forever. At that event he made the decision to Go Pro and become a Network Marketing expert." }
		//[Authorize(Roles = "Admin")]
		public async Task<IHttpActionResult> Post(TranslatedPhoto translatedPhoto, [ValueProvider(typeof(CultureValueProviderFactory))] string culture = "en-US")
		{
			translatedPhoto.Culture = culture;

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

			try
			{
				var photo = new Photo(translatedPhoto, _cultureManager.SupportedCultures);
				var newPhoto = _photosManager.Post(photo);

				await _photosManager.SaveChanges();
				translatedPhoto.Id = newPhoto.Id;
				return Created(translatedPhoto);
			}
			catch (Exception ex)
			{
				throw;
			}
		}
		// PUT  http://localhost:50481/odata/Photos('go-pro-7-steps')
		// User-Agent: Fiddler
		// Host: localhost:14270
		// Content-type: application/json
		// Accept: application/json
		// Content-Length: 34
		// { id:"go-pro-7-steps", title:"Go Pro: 7 Steps to Becoming a Network Marketing Professional [PUT]", description:"Over twenty years ago at a company convention, Eric Worre had an aha moment that changed his life forever. At that event he made the decision to Go Pro and become a Network Marketing expert." }
		//[Authorize(Roles = "Admin")]
		public async Task<IHttpActionResult> Put([FromODataUri] string key, TranslatedPhoto updatePhoto, [ValueProvider(typeof(CultureValueProviderFactory))] string culture = "en-US")
		{
			updatePhoto.Culture = culture;

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

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

			var photo = new Photo(updatePhoto, new[] { culture });
			var modelPhoto = _photosManager.Put(key, photo);

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

		}
		public async Task<IHttpActionResult> Patch([FromODataUri] string key, Delta<TranslatedPhoto> delta, [ValueProvider(typeof(CultureValueProviderFactory))] string culture = "en-US")
		{
			var photo = new Photo(delta.GetEntity(), culture);
			var tochange = _photosManager.Patch(key, photo);

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

			return Updated(tochange);

		}