public async Task <IActionResult> AddFile([FromRoute] string sub, IFormFile model) { if (sub != User.FindFirst("sub").Value) { return(Forbid()); } var fileEntity = new File { Name = model.Name, OwnerSub = sub }; using (var memoryStream = new MemoryStream()) { await model.CopyToAsync(memoryStream); fileEntity.Data = memoryStream.ToArray(); } var entity = await _context.Files.AddAsync(fileEntity); await entity.Reference(x => x.OwnerProfile).LoadAsync(); await _context.SaveChangesAsync(); return(Ok(fileEntity.ToFileViewModel())); }
public async Task <IActionResult> PutCar(int id, Car car) { if (id != car.Id) { return(BadRequest()); } _context.Entry(car).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CarExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> AddBlog([FromBody] BlogCreateViewModel blogDefinition) { var sub = User.FindFirst("sub").Value; var entity = await _context.Blogs.AddAsync(new Blog { Key = blogDefinition.Key, DisplayName = blogDefinition.Name, Description = blogDefinition.Description, OwnerSub = sub }); await entity.Reference(x => x.OwnerProfile).LoadAsync(); try { await _context.SaveChangesAsync(); } catch { entity.State = EntityState.Detached; return(BadRequest($"{blogDefinition.Key} is already taken")); } var blog = entity.Entity; return(Ok(blog.ToBlogViewModel())); }
public async Task <IActionResult> AddPublicationComments([FromRoute] int blogId, [FromRoute] int publicationId, [FromBody] CommentCreateViewModel model) { var publication = await _context.Publications.SingleOrDefaultAsync(p => p.BlogId == blogId && p.Id == publicationId); if (publication == null) { return(NotFound()); } var entity = await _context.Messages.AddAsync(new Message { BlogId = blogId, PublicationId = publicationId, Body = model.Body, Subject = model.Subject }); entity.Reference(m => m.SenderProfile).Load(); await _context.SaveChangesAsync(); return(Ok(entity.Entity.ToCommentViewModel())); }
public async Task <IActionResult> UpdateUser([FromRoute] string sub, UserUpdateViewmodel model) { if (sub != User.FindFirst("sub").Value) { return(Forbid()); } var user = await _context.UserProfiles.SingleOrDefaultAsync(u => u.Sub == sub); if (user == null) { return(NotFound()); } user.DisplayName = model.Name; await _context.SaveChangesAsync(); return(Ok(new UserViewModel { Name = user.DisplayName, RouteValues = new { sub = user.Sub } })); }