// GET: api/Users/5 public HttpResponseMessage Get(int id) { try { using (var context = new PicContext()) { var user = context .Users .Single(u => u.Id == id); var res = new UserDto { Id = user.Id, Email = user.Email, Info = user.Info, Login = user.Login }; return Request.CreateResponse<UserDto>(HttpStatusCode.OK, res); } } catch (InvalidOperationException) { return Request.CreateResponse(HttpStatusCode.NotFound); } catch (ArgumentNullException) { return Request.CreateResponse(HttpStatusCode.BadRequest); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } }
public HttpResponseMessage Get(int startPage, int perPage) { try { IEnumerable<string> headers; string authorIdValue = null; if (Request.Headers.TryGetValues(Headers.UserId, out headers)) authorIdValue = headers.SingleOrDefault(); var authorId = string.IsNullOrEmpty(authorIdValue) ? (long?)null : long.Parse(authorIdValue); using (var context = new PicContext()) { PageDto<PictureDto> resPageDto = new PageDto<PictureDto>(); IQueryable<Picture> pictures = context .Pictures; if (authorId != null) { pictures = pictures.Where(p => p.AuthorId == authorId); } resPageDto.TotalItems = pictures.Count(); var pictureList = pictures .OrderBy(u => u.Id) .Skip((startPage - 1) * perPage) .Take(perPage) .ToList(); List<PictureDto> resPictures = PictureHelper.DtoToPicture(pictureList); resPageDto.Items = resPictures; return Request.CreateResponse<PageDto<PictureDto>>(HttpStatusCode.OK, resPageDto); } } catch (Exception) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } }
// GET: api/Users public HttpResponseMessage Get(int startPage, int perPage) { try { using (var context = new PicContext()) { var res = context .Users .OrderBy(u => u.Id) .Skip((startPage - 1)*perPage) .Take(perPage) .Select(u => new UserDto { Id = u.Id, Email = u.Email, Info = u.Info, Login = u.Login }).ToList(); return Request.CreateResponse<List<UserDto>>(HttpStatusCode.OK, res); } } catch (Exception) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } }
public HttpResponseMessage Get(int id) { try { using (var context = new PicContext()) { var res = new PictureDto(context .Pictures .Single(u => u.Id == id)); return Request.CreateResponse<PictureDto>(HttpStatusCode.OK, res); } } catch (InvalidOperationException) { return Request.CreateResponse(HttpStatusCode.NotFound); } catch (ArgumentNullException) { return Request.CreateResponse(HttpStatusCode.BadRequest); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } }
// GET: api/Albums/5 public Album Get(int id) { using (var context = new PicContext()) { return context.Albums.Single(a => a.Id == id); } }
// 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(); } }
// 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(); } }
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(); } }
private long? CheckUser(string login, string password) { using (var context = new PicContext()) { var user = context.Users.FirstOrDefault(u => u.Login == login || u.Email == login); if (user != null) { var hash = HashHelper.ComputeHash(password, user.Salt); if (hash.SequenceEqual(user.Hash)) return user.Id; } } return null; }
// 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); } }
// GET: api/Albums public IEnumerable<Album> Get(int startPage, int perPage) { var ownerIdValue = Request.Headers.GetValues(Headers.UserId).SingleOrDefault(); var ownerId = string.IsNullOrEmpty(ownerIdValue) ? (long?)null : long.Parse(ownerIdValue); using (var context = new PicContext()) { IQueryable<Album> pictures = context .Albums; if (ownerId != null) pictures = pictures.Where(p => p.OwnerId == ownerId); return pictures .OrderBy(u => u.Id) .Skip((startPage - 1) * perPage) .Take(perPage) .ToList(); } }
// 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); } }
// 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); } }
// 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/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/Users/5 public HttpResponseMessage Delete(int id) { try { using (var context = new PicContext()) { var user = context.Users.Single(u => u.Id == id); context.Users.Remove(user); 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); } }