public async Task <IHttpActionResult> PostCustomer(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            customer.TrackingState = TrackingState.Added;
            _dbContext.ApplyChanges(customer);


            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (_dbContext.Customers.Any(c => c.CustomerId == customer.CustomerId))
                {
                    return(Conflict());
                }
                throw;
            }

            await _dbContext.LoadRelatedEntitiesAsync(customer);

            customer.AcceptChanges();
            return(CreatedAtRoute("DefaultApi", new { id = customer.CustomerId }, customer));
        }
        public async Task <IActionResult> PutOrder([FromBody] Order order)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Apply changes to context
            _context.ApplyChanges(order);

            try
            {
                // Persist changes
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_context.Orders.Any(o => o.OrderId == order.OrderId))
                {
                    return(NotFound());
                }
                throw;
            }

            // Populate reference properties
            await _context.LoadRelatedEntitiesAsync(order);

            // Reset tracking state to unchanged
            _context.AcceptChanges(order);

            //return NoContent();
            return(Ok(order));
        }
        public async Task <IHttpActionResult> PostProduct(Product entity)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            entity.TrackingState = TrackingState.Added;
            _dbContext.ApplyChanges(entity);


            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (_dbContext.Products.Any(e => e.ProductId == entity.ProductId))
                {
                    return(Conflict());
                }
                throw;
            }

            await _dbContext.LoadRelatedEntitiesAsync(entity);

            entity.AcceptChanges();
            return(CreatedAtRoute("DefaultApi", new { id = entity.ProductId }, entity));
        }
        public async Task <ActionResult <Product> > PostProduct(Product product)
        {
            _context.Entry(product).State = EntityState.Added;
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetProduct), new { id = product.ProductId }, product));
        }
Exemplo n.º 5
0
        public async Task <IHttpActionResult> PostTerritory(Territory territory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            territory.TrackingState = TrackingState.Added;
            _dbContext.ApplyChanges(territory);


            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (_dbContext.Territories.Any(t => t.TerritoryId == territory.TerritoryId))
                {
                    return(Conflict());
                }
                throw;
            }

            await _dbContext.LoadRelatedEntitiesAsync(territory);

            territory.AcceptChanges();
            return(CreatedAtRoute("DefaultApi", new { id = territory.TerritoryId }, territory));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> PostProduct([FromBody] Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Entry(product).State = EntityState.Added;
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetProduct", new { id = product.ProductId }, product));
        }
        public async Task <IHttpActionResult> PostOrder(Order order)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            order.TrackingState = TrackingState.Added;
            _dbContext.ApplyChanges(order);

            await _dbContext.SaveChangesAsync();

            await _dbContext.LoadRelatedEntitiesAsync(order);

            order.AcceptChanges();
            return(CreatedAtRoute("DefaultApi", new { id = order.OrderId }, order));
        }
Exemplo n.º 8
0
        public async Task <IHttpActionResult> PostEmployee(Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            employee.TrackingState = TrackingState.Added;
            _dbContext.ApplyChanges(employee);

            await _dbContext.SaveChangesAsync();

            await _dbContext.LoadRelatedEntitiesAsync(employee);

            employee.AcceptChanges();
            return(CreatedAtRoute("DefaultApi", new { id = employee.EmployeeId }, employee));
        }
        public async Task <Customer> CreateCustomer(Customer customer)
        {
            customer.TrackingState = TrackingState.Added;
            _dbContext.ApplyChanges(customer);

            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateException updateEx)
            {
                throw new FaultException(updateEx.Message);
            }

            await _dbContext.LoadRelatedEntitiesAsync(customer);

            customer.AcceptChanges();
            return(customer);
        }
Exemplo n.º 10
0
        public async Task <Order> CreateOrder(Order order)
        {
            // Mark order as added
            order.TrackingState = TrackingState.Added;
            _dbContext.ApplyChanges(order);

            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateException updateEx)
            {
                throw new FaultException(updateEx.Message);
            }

            // Load related entities and accept changes
            await _dbContext.LoadRelatedEntitiesAsync(order);

            order.AcceptChanges();
            return(order);
        }
Exemplo n.º 11
0
        public async Task <IActionResult> Post([FromBody] Product item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Entry(item).State = EntityState.Added;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (ItemExists(item.ProductId))
                {
                    return(StatusCode((int)HttpStatusCode.Conflict));
                }
                throw;
            }

            return(Created(item));
        }