public HttpResponseMessage ChangePhoto(string photoInBinary) { try { if (photoInBinary == null) { return(new HttpResponseMessage(HttpStatusCode.NotAcceptable)); } byte[] ImageData = PictureHelper.ConvertToImage(photoInBinary); var userId = User.Claims.FirstOrDefault(C => C.Type == ClaimTypes.NameIdentifier).Value; var user = _context.Users.FirstOrDefault(u => u.Id == int.Parse(userId)); if (user != null) { user.Picture = new Picture(); user.Picture.ImageData = ImageData; _context.SaveChanges(); return(new HttpResponseMessage(HttpStatusCode.OK)); } else { return(new HttpResponseMessage(HttpStatusCode.NotAcceptable)); } } catch (FormatException ex) { return(new HttpResponseMessage(HttpStatusCode.NotAcceptable)); } catch (Exception ex) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } }
public HttpResponseMessage EditUserPicture([FromBody] string imageData) { try { var userId = User.Claims.FirstOrDefault(C => C.Type == ClaimTypes.NameIdentifier).Value; var user = _context.Users.Where(u => u.Id == int.Parse(userId)) .Include(u => u.Picture) .FirstOrDefault(); if (user != null) { byte[] ImageData = null; if (imageData != null) { ImageData = PictureHelper.ConvertToImage(imageData); user.Picture = new Picture() { ImageData = ImageData }; _context.SaveChanges(); } return(new HttpResponseMessage(HttpStatusCode.OK)); } else { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } } catch (Exception ex) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } }
public HttpResponseMessage AddBook([FromBody] BookModel incomingBook) { try { var userId = User.Claims.FirstOrDefault(C => C.Type == ClaimTypes.NameIdentifier).Value; var user = _context.Users.FirstOrDefault(u => u.Id == int.Parse(userId)); if (user != null) { byte[] ImageData = null; if (incomingBook.photoInBinary != null) { ImageData = PictureHelper.ConvertToImage(incomingBook.photoInBinary); } Book book = new Book { CurrentUserId = user.Id, ContributorId = user.Id, Title = incomingBook.name, Author = incomingBook.author, Price = Convert.ToDecimal(incomingBook.price), Picture = ImageData != null ? new Data.Models.Picture() { ImageData = ImageData, Name = incomingBook.photo } : null, BookTypeId = 1, //incomingBook.type, GenreId = 1, //incomingBook.genre IsUsable = true, CreatedOn = DateTime.UtcNow }; _context.Books.Add(book); _context.SaveChanges(); BookHistory bookHistory = new BookHistory { BookId = book.Id, GetBookOn = DateTime.UtcNow, UserId = user.Id, AltitudeCoordinate = incomingBook.AltitudeCoordinate, LongtiudeCoordinate = incomingBook.LongitudeCoordinate }; _context.BookHistoryRecords.Add(bookHistory); _context.SaveChanges(); return(new HttpResponseMessage(HttpStatusCode.Created)); } else { return(new HttpResponseMessage(HttpStatusCode.Unauthorized)); } } catch (Exception e) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } }