public async Task <BottleType> AddBottleType(CreateBottleTypeVM bottleType) { try { var newBottleType = new BottleType() { Color = bottleType.Color, YearVintage = bottleType.YearVintage, Winery = bottleType.Winery, Varietal = bottleType.Varietal, Vinyard = bottleType.Vinyard, Name = bottleType.Name, PurchaseDate = bottleType.PurchaseDate, PurchasePrice = bottleType.PurchasePrice, StorageDate = bottleType.StorageDate, StorageTemparature = bottleType.StorageTemparature, CreatedById = bottleType.CreatedById, OpenedDate = bottleType.OpenedDate, Notes = bottleType.Notes }; for (var i = 0; i < bottleType.NewBottleCount; i++) { var newBottle = new Bottle() { Color = bottleType.Color, YearVintage = bottleType.YearVintage, Winery = bottleType.Winery, Varietal = bottleType.Varietal, Vinyard = bottleType.Vinyard, Name = bottleType.Name, PurchaseDate = bottleType.PurchaseDate, PurchasePrice = bottleType.PurchasePrice, StorageDate = bottleType.StorageDate, StorageTemparature = bottleType.StorageTemparature, CreatedById = bottleType.CreatedById, CreatedDateTime = bottleType.CreatedDateTime, OpenedDate = bottleType.OpenedDate, Notes = bottleType.Notes }; newBottleType.Bottles.Add(newBottle); } _ctx.BottleTypes.Add(newBottleType); await _ctx.SaveChangesAsync(); return(newBottleType); } catch (Exception e) { throw e; } }
public async Task <IActionResult> Post([FromBody] CreateBottleTypeVM bottleType) { try { var newBottle = await _bottleService.AddBottleType(bottleType); return(Ok(newBottle)); } catch { return(StatusCode(500, "Internal server error")); } }
public async Task <Bottle> AddBottle(Guid bottleTypeId, BottleVM bottle) { try { var bottleType = await _ctx.BottleTypes.FindAsync(bottleTypeId); if (bottleType == null) { var newBottleType = new CreateBottleTypeVM() { Color = bottle.Color, YearVintage = bottle.YearVintage, Winery = bottle.Winery, Varietal = bottle.Varietal, Vinyard = bottle.Vinyard, Name = bottle.Name, PurchaseDate = bottle.PurchaseDate, PurchasePrice = bottle.PurchasePrice, StorageDate = bottle.StorageDate, StorageTemparature = bottle.StorageTemparature, CreatedById = bottle.CreatedById, CreatedDateTime = bottle.CreatedDateTime, OpenedDate = bottle.OpenedDate, Notes = bottle.Notes, NewBottleCount = 1 }; var createdBottleType = await AddBottleType(newBottleType); return(createdBottleType.Bottles.FirstOrDefault()); } else { var newBottle = new Bottle() { BottleTypeId = bottleTypeId, Color = bottle.Color, YearVintage = bottle.YearVintage, Winery = bottle.Winery, Varietal = bottle.Varietal, Vinyard = bottle.Vinyard, Name = bottle.Name, PurchaseDate = bottle.PurchaseDate, PurchasePrice = bottle.PurchasePrice, StorageDate = bottle.StorageDate, StorageTemparature = bottle.StorageTemparature, CreatedById = bottle.CreatedById, CreatedDateTime = bottle.CreatedDateTime, OpenedDate = bottle.OpenedDate, Notes = bottle.Notes }; _ctx.Bottles.Add(newBottle); await _ctx.SaveChangesAsync(); return(newBottle); } } catch (Exception e) { throw e; } }