public async Task <ActionResult> Post([FromBody] CreationBranchDTO creationBranchDTO) { var entity = mapper.Map <Branch>(creationBranchDTO); context.Add(entity); await context.SaveChangesAsync(); var branchDTO = mapper.Map <BranchDTO>(entity); return(new CreatedAtRouteResult("GetBranch", new { id = branchDTO.Id }, branchDTO)); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { using (PizzaShopDbContext pizzaShopDbContext = app.ApplicationServices.CreateScope() .ServiceProvider.GetRequiredService <PizzaShopDbContext>()) { try { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); pizzaShopDbContext.Database.EnsureDeleted(); pizzaShopDbContext.Database.EnsureCreated(); pizzaShopDbContext.Add(new Customer { PhoneNumber = "1111111111", Password = "******", Name = "Surafel Assefa", Address = "White House", PaymentType = PaymentType.VisaCard, }); pizzaShopDbContext.SaveChanges(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); pizzaShopDbContext.Database.Migrate(); } } catch (PostgresException e) { throw e; } } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseSession(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); }
public async Task <ActionResult> Post([FromForm] CreationProductDTO creationProductDTO) { var entity = mapper.Map <Product>(creationProductDTO); context.Add(entity); using (var stream = new MemoryStream()) { await creationProductDTO.Image.CopyToAsync(stream); entity.Image = Convert.ToBase64String(stream.ToArray()); } await context.SaveChangesAsync(); var dto = mapper.Map <ProductDTO>(entity); return(new CreatedAtRouteResult("GetProduct", new { id = entity.Id }, dto)); }
private void AddEntity <T>(T entity) { _pizzaShopDbContext.Add(entity); _pizzaShopDbContext.SaveChanges(); }