public async Task <IActionResult> Post( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "Contact")] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger POST Contact function processed a request."); try { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var contact = JsonConvert.DeserializeObject <Contact>(requestBody); if (contact == null) { return(new BadRequestResult()); } _context.Contacts.Add(contact); await _context.SaveChangesAsync(); return(new OkObjectResult(contact)); } catch (Exception e) { return(new BadRequestObjectResult(e.Message)); } }
public async Task <IActionResult> PutEmployee(int id, Employee employee) { if (id != employee.Id) { return(BadRequest()); } _context.Entry(employee).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> CreateCustomer(Customer customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (customer == null) { return(BadRequest("Customer data is empty")); } if (customer.CustomerID == null || customer.CustomerID?.Length == 0) { return(BadRequest("Customer ID is empty")); } var query = _dbContext.Customers.Where(c => c.CustomerID == customer.CustomerID); if (query.Any()) { return(BadRequest("Customer exists")); } var customerEntity = Mapper.Map <Customer, CustomerEntity>(customer); var entry = await _dbContext.AddAsync(customerEntity); await _dbContext.SaveChangesAsync(); return(CreatedAtAction(nameof(GetByCustomerId), new { id = customer.CustomerID }, customer)); }
public async Task <ActionResult> PostProducts(Scheduler scheduler) { _context.Scheduler.Add(scheduler); await _context.SaveChangesAsync(); return(CreatedAtAction("GetProducts", new { id = scheduler.TaskId }, scheduler)); }
public async Task <IActionResult> Create([Bind("Id,UserId,Title,Body")] Sample sample) { if (ModelState.IsValid) { _context.Add(sample); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(sample)); }
public async Task Handle(TCommand command) { await decorated.Handle(command); await dbcontext.SaveChangesAsync(); //unitOfWork.Commit(); }
public async Task <BooksModel> AddNewBook(BooksModel bookModel) { /** Identity Value */ // var newID = _context.BooksDbSet.Select(x => x.ID).Max() + 1; // bookModel.ID = newID; try { _context.BooksDbSet.Add(bookModel); await _context.SaveChangesAsync(); return(bookModel); } catch (Exception ex) { // Log exception return(bookModel); } }