public Photo(TranslatedPhoto translatedPhoto, IEnumerable<string> cultures) : this() { Id = translatedPhoto.Id; foreach (var culture in cultures) { Translations.Add(new PhotoTranslation { Culture = culture, PhotoId = translatedPhoto.Id, Title = translatedPhoto.Title, Description = translatedPhoto.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; } }
public Photo(TranslatedPhoto translatedPhoto, string culture) : this(translatedPhoto, new[] { culture }) { }
// 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); }