public async Task <IActionResult> AddTray([FromRoute] long boxId, [FromBody] TrayCreationData creationData) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var tray = await _boxService.AddTray(User.GetId(), boxId, creationData); return(CreatedAtAction(nameof(GetTray), new { boxId, trayId = tray.Id }, tray)); } catch (Exception e) { return(BadRequest()); } }
public async Task <TrayDto> AddTray(Guid userId, long boxId, TrayCreationData creationData) { using (Context) { //var user = await GetUserById(userId); var box = await Context.Boxes .Include(b => b.User) .FirstOrDefaultAsync(b => b.Id == boxId && b.User.Guid == userId); var tray = new Tray { Box = box, User = box.User, Name = creationData.Name }; var newTray = await Context.Trays.AddAsync(tray); await Context.SaveChangesAsync(); return(newTray.Entity.ToTrayDto()); } }