// POST: api/Albums public void Post([FromBody]Album value) { using (var context = new PicContext()) { context.Albums.Add(value); context.SaveChanges(); } }
public void AddPicture(long albumId, long pictureId) { using (var context = new PicContext()) { context.AlbumPictures.Add(new AlbumPicture {AlbumId = albumId, PictureId = pictureId}); context.SaveChanges(); } }
public void DeletePicture(long albumId, long pictureId) { using (var context = new PicContext()) { var albumPicture = context.AlbumPictures.FirstOrDefault(ap => ap.AlbumId == albumId && ap.PictureId == pictureId); if (albumPicture != null) { context.AlbumPictures.Remove(albumPicture); } context.SaveChanges(); } }
// POST: api/Pictures public HttpResponseMessage Post([FromBody]PictureDto value) { try { using (var context = new PicContext()) { context.Pictures.Add(PictureHelper.DtoToPicture(value)); context.SaveChanges(); return Request.CreateResponse(HttpStatusCode.Created); } } catch (ArgumentNullException) { return Request.CreateResponse(HttpStatusCode.BadRequest); } catch (NullReferenceException) { return Request.CreateResponse(HttpStatusCode.BadRequest); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } }
// DELETE: api/Albums/5 public void Delete(int id) { using (var context = new PicContext()) { var album = context.Albums.Single(p => p.Id == id); context.Albums.Remove(album); context.SaveChanges(); } }
// PUT: api/Albums/5 public void Put(int id, [FromBody]Album value) { using (var context = new PicContext()) { var album = context.Albums.Single(a => a.Id == id); album.Description = string.IsNullOrEmpty(value.Description) ? album.Description : value.Description; album.Title = string.IsNullOrEmpty(value.Title) ? album.Title : value.Title; context.SaveChanges(); } }
// POST: api/Users public HttpResponseMessage Post([FromBody]UserDto user) { try { var password = Request.Headers.GetValues("password").Single(); var salt = HashHelper.CreateSalt(); using (var context = new PicContext()) { if (context.Users.Count(n => n.Email == user.Email) > 0) { return Request.CreateResponse<UserDto>(HttpStatusCode.Conflict, new UserDto()); } context.Users.Add(new User { Email = user.Email, Hash = HashHelper.ComputeHash(password, salt), Salt = salt, Login = user.Login, Info = user.Info }); context.SaveChanges(); } using (var context = new PicContext()) { var result = context .Users .Single(u => u.Email == user.Email); var newUser = new UserDto { Id = result.Id, Email = result.Email, Info = result.Info, Login = result.Login }; return Request.CreateResponse<UserDto>(HttpStatusCode.Created, newUser); } } catch (ArgumentNullException) { return Request.CreateResponse(HttpStatusCode.BadRequest); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } }
// PUT: api/Users/5 public HttpResponseMessage Put(int id, [FromBody]UserDto value) { try { using (var context = new PicContext()) { var user = context.Users.Single(u => u.Id == id); user.Email = value.Email ?? user.Email; user.Info = value.Info ?? user.Info; context.SaveChanges(); var r = new UserDto { Id = user.Id, Email = user.Email, Info = user.Info, Login = user.Login }; return Request.CreateResponse<UserDto>(HttpStatusCode.Accepted, r); } } catch (InvalidOperationException) { return Request.CreateResponse(HttpStatusCode.NotFound); } catch (ArgumentNullException) { return Request.CreateResponse(HttpStatusCode.BadRequest); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } }
// DELETE: api/Pictures/5 public HttpResponseMessage Delete(int id) { try { using (var context = new PicContext()) { var pic = context.Pictures.Single(p => p.Id == id); context.Pictures.Remove(pic); context.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK); } } catch (InvalidOperationException) { return Request.CreateResponse(HttpStatusCode.NotFound); } catch (ArgumentNullException) { return Request.CreateResponse(HttpStatusCode.BadRequest); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } }
// PUT: api/Pictures/5 public HttpResponseMessage Put([FromBody]PictureDto value) { try { using (var context = new PicContext()) { var pic = context.Pictures.Single(u => u.Id == value.Id); pic.Description = value.Description ?? pic.Description; pic.Title = value.Title ?? pic.Title; context.SaveChanges(); return Request.CreateResponse(HttpStatusCode.Accepted); } } catch (InvalidOperationException) { return Request.CreateResponse(HttpStatusCode.NotFound); } catch (ArgumentNullException) { return Request.CreateResponse(HttpStatusCode.BadRequest); } catch (NullReferenceException) { return Request.CreateResponse(HttpStatusCode.BadRequest); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } }