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

            Customer newCustomer = new Customer();

            newCustomer.Name    = customer.Name;
            newCustomer.Phone   = customer.Phone;
            newCustomer.Email   = customer.Email;
            newCustomer.Address = customer.Address;
            newCustomer.Zip     = customer.Zip;
            newCustomer.City    = customer.City;
            newCustomer.Country = customer.Country;
            newCustomer.ShopId  = customer.ShopId;

            db.Customers.Add(newCustomer);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                throw;
            }

            return(CreatedAtRoute("WSApi", new { id = newCustomer.Id }, newCustomer));
        }
 public CustomerDTO(CustomerIn customerIn)
 {
     Name          = customerIn.Name;
     MobileNumber  = customerIn.MobileNumber;
     Email         = customerIn.Email;
     PendingAmount = customerIn.PendingAmount;
     TotalAmount   = customerIn.PendingAmount;
 }
Ejemplo n.º 3
0
        public async Task <ActionResult <CustomerOut> > PostCustomer([FromForm] CustomerIn customerIn)
        {
            var customerDto = new CustomerDTO(customerIn);

            _context.Customers.Add(customerDto);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetCustomer", new { id = customerDto.ID }, customerDto));
        }
        public async Task <IHttpActionResult> PutCustomer(long id, CustomerIn customerIn)
        {
            CurrentIdentity identity = getIdentity();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Customer customerCurrent = await db.Customers.FindAsync(id);

            if (customerCurrent == null)
            {
                return(ResponseMessage(getHttpResponse(HttpStatusCode.NotFound)));
            }

            if (identity.role != "Admin")
            {
                if (customerCurrent.ShopId != (await db.Shops.FirstOrDefaultAsync(p => p.UserId == identity.userId)).Id)
                {
                    return(ResponseMessage(getHttpResponse(HttpStatusCode.Unauthorized)));
                }
            }

            customerCurrent.Name    = (customerIn.Name != null) ? customerIn.Name : customerCurrent.Name;
            customerCurrent.Phone   = (customerIn.Phone != null) ? customerIn.Phone : customerCurrent.Phone;
            customerCurrent.Email   = (customerIn.Email != null) ? customerIn.Email : customerCurrent.Email;
            customerCurrent.Address = (customerIn.Address != null) ? customerIn.Address : customerCurrent.Address;
            customerCurrent.Zip     = (customerIn.Zip != null) ? customerIn.Zip : customerCurrent.Zip;
            customerCurrent.City    = (customerIn.City != null) ? customerIn.City : customerCurrent.City;
            customerCurrent.Country = (customerIn.Country != null) ? customerIn.Country : customerCurrent.Country;
            customerCurrent.ShopId  = customerCurrent.ShopId;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 5
0
 public CustomerDTO(CustomerIn customerIn) : base(customerIn)
 {
 }