コード例 #1
0
        public async Task <ActionResult> CreateCustomer([FromBody] CustomerForCreationDto customerForCreationDto)
        {
            if (ModelState.IsValid)
            {
                var customerToCreate = _mapper.Map <Customer>(customerForCreationDto);
                _repo.Add(customerToCreate);

                if (await _repo.SaveChangesAsync())
                {
                    var customerToReturn = _mapper.Map <CustomerForReturnDto>(customerToCreate);
                    return(CreatedAtRoute("GetCustomer",
                                          new { controller = "Customers", id = customerToCreate.CustomerId }, customerToReturn));
                }
                else
                {
                    return(BadRequest("Failed to save changes to the database."));
                }
            }

            return(BadRequest(ModelState));

            /*if (await _repo.SaveAll()) {
             *  var customerToRetun = _mapper.Map<CustomerForReturnDto>(customer);
             *  return CreatedAtAction(nameof(GetCustomer), new { id = customer.CustomerId }, customerToRetun);
             * }*/
        }
コード例 #2
0
        public ActionResult CreateCustomer([FromBody] CustomerForCreationDto customerForCreation)
        {
            if (customerForCreation == null)
            {
                _logger.LogWarning("An invalid customer object was received for create");
                return(BadRequest());
            }
            else
            {
                _logger.LogInformation("Starting to process the new customer");
            }

            var customerRepo = AutoMapper.Mapper.Map <Customer>(customerForCreation);

            if (!ModelState.IsValid)
            {
                _logger.LogInformation("Creating new customer failed validation");
                return(BadRequest(ModelState));
            }

            _customerRepository.AddCustomer(customerRepo);

            if (!_customerRepository.Save())
            {
                //Global error handler will return expected status code
                throw new Exception("Creating customer failed on save");
            }

            var customerToReturn = AutoMapper.Mapper.Map <CustomerDto>(customerRepo);

            _logger.LogInformation($"New customer with Id {customerToReturn.CustomerId} was created");
            return(CreatedAtRoute("GetCustomer",
                                  new { customerid = customerToReturn.CustomerId },
                                  customerToReturn));
        }
コード例 #3
0
        public async Task <ActionResult <CustomerDto> > Post([FromBody] CustomerForCreationDto customerForCreationDto)
        {
            if (await this._customerRepo.IsCustomerExist(customerForCreationDto.Email))
            {
                return(BadRequest());
            }

            var customer = new Customer
            {
                FirstName   = customerForCreationDto.FirstName,
                LastName    = customerForCreationDto.LastName,
                DateOfBirth = customerForCreationDto.DateOfBirth,
                Email       = customerForCreationDto.Email,
                Mobile      = customerForCreationDto.Mobile,
                Telephone   = customerForCreationDto.Telephone,
                Title       = customerForCreationDto.Title
            };


            Customer model = await _customerRepo.Create(customer);

            var customerDto = _mapper.Map <CustomerDto>(model);

            return(CreatedAtAction(nameof(GetById), new { id = customerDto.Id }, customerDto));
        }
コード例 #4
0
        public async Task <ActionResult <Customer> > PostCustomer([FromBody] CustomerForCreationDto customerForCreate)
        {
            try
            {
                if (customerForCreate == null)
                {
                    return(BadRequest("Customer object is null"));
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Invalid model object"));
                }
                var customerEntity = _mapper.Map <Customer>(customerForCreate);

                _repo.Add(customerEntity);

                await _repo.SaveAll();

                return(Ok(customerEntity));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal server error"));
            }
        }
コード例 #5
0
        public async Task <IActionResult> CreateCustomer(CustomerForCreationDto creationDto)
        {
            var accountId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var result    = await _customerService.CreateCustomerAsync(creationDto, accountId);

            return(StatusCode((int)result.Code, result));
        }
コード例 #6
0
        public async Task <ApiResult <string> > CreateCustomerAsync(CustomerForCreationDto creationDto, string accountId)
        {
            try
            {
                var checkEmployee = await _context.Employees.Where(x => x.AppuserId.ToString() == accountId)
                                    .SingleOrDefaultAsync();

                if (checkEmployee == null)
                {
                    return(new ApiResult <string>(HttpStatusCode.NotFound, "Lỗi tài khoản đăng nhập"));
                }
                var sequenceNumber = await _context.Customers.CountAsync();

                var customerId = IdentifyGenerator.GenerateCustomerId(sequenceNumber + 1);
                var customer   = ObjectMapper.Mapper.Map <Data.Entities.Customer>(creationDto);
                customer.Id         = customerId;
                customer.EmployeeId = checkEmployee.Id;
                await _context.Customers.AddAsync(customer);

                await _context.SaveChangesAsync();

                return(new ApiResult <string>(HttpStatusCode.OK)
                {
                    ResultObj = customerId, Message = "Tạo mới khách hàng thành công"
                });
            }
            catch
            {
                return(new ApiResult <string>(HttpStatusCode.InternalServerError, "Có lỗi khi tạo khách hàng"));
            }
        }
コード例 #7
0
        public async Task <IActionResult> AddCustomer([FromBody] CustomerForCreationDto model)
        {
            var customer      = _mapper.Map <Customer>(model);
            var customerAdded = _mapper.Map <CustomerDto>(await _customerService.AddAsync(customer));

            return(CreatedAtRoute("GetCustomer",
                                  new { id = customerAdded.CustomerID },
                                  customerAdded));
        }
コード例 #8
0
        public IActionResult CreateCustomer([FromBody] CustomerForCreationDto customer)
        {
            if (customer == null)
            {
                return(BadRequest());
            }

            return(Ok());
        }
コード例 #9
0
        public async Task <ActionResult <CustomerDto> > CreateCustomer([FromBody] CustomerForCreationDto customer)
        {
            var customerToAdd = _mapper.Map <Customer>(customer);

            _customersRepository.Add(customerToAdd);
            await _customersRepository.SaveAsync();

            var customerToReturn = _mapper.Map <CustomerDto>(customerToAdd);

            return(CreatedAtAction(nameof(GetCustomer), new { customerId = customerToReturn.Id }, customerToReturn));
        }
コード例 #10
0
        public IActionResult UpdateCustomer([FromRoute] int customerId, [FromBody] CustomerForCreationDto customer)
        {
            if (customer == null)
            {
                return(BadRequest());
            }

            var customerEntity = _customerInfoRepository.GetCustomer(customerId);

            if (customerEntity == null)
            {
                return(NotFound());
            }


            if (customer.Name != null)
            {
                customerEntity.Name = customer.Name;
            }
            if (customer.Name.Length > 50)
            {
                return(StatusCode(500, "The name you entered is a tad bit too long...."));
            }
            if (customer.MobileNr.Length > 10)
            {
                return(StatusCode(418, "Teapot error, go away. Actually your mobile number is just too long"));
            }
            if (customer.Adress != null)
            {
                customerEntity.Adress = customer.Adress;
            }
            if (customer.Adress.Length > 50)
            {
                return(StatusCode(500, "The adress is too long..............."));
            }


            if (customer.CreatedAtDate != null)
            {
                customerEntity.CreatedAtDate = customer.CreatedAtDate;
            }

            customer.UpdatedAtDate = DateTime.Now;


            if (!_customerInfoRepository.Save())
            {
                return(StatusCode(418, "Something happened maatjie. Ek is jammer, kyk na die tee pot, want jou data wil nie save nie"));
            }


            return(Ok(customerEntity));
        }
コード例 #11
0
        public ActionResult <CustomerDto> CreateCustomer(CustomerForCreationDto customer)
        {
            var customerEntity = _mapper.Map <Customer>(customer);

            _unitOfWork.Customers.Add(customerEntity);
            _unitOfWork.Commit();

            var customerToReturn = _mapper.Map <CustomerDto>(customerEntity);

            return(CreatedAtRoute(
                       routeName: nameof(GetCustomer),
                       routeValues: new { customerId = customerToReturn.Id },
                       value: customerToReturn));
        }
コード例 #12
0
        public async Task <IActionResult> UpdateDepartment(int id, CustomerForCreationDto customerForClientDto)
        {
            int userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            Customer customerFromRepo = await _repo.GetSingleCustomer(userId, id);

            _mapper.Map(customerForClientDto, customerFromRepo);

            if (await _repo.SaveAll())
            {
                var customerToReturn = _mapper.Map <CustomerForReturnDto>(customerFromRepo);
                return(CreatedAtRoute("GetCustomer", new { id = customerFromRepo.Id }, customerToReturn));
            }

            throw new Exception("Updating customer failed on save");
        }
コード例 #13
0
        public ActionResult <CustomerDto> CreateCustomer(CustomerForCreationDto customer)
        {
            var customerEntities = this.mapper.Map <Customer>(customer);

            this.customersRepository.AddCustomer(customerEntities);
            this.customersRepository.Save();

            var customerToReturn = this.mapper.Map <CustomerDto>(customerEntities);

            return(CreatedAtRoute("GetCustomer",
                                  new
            {
                customerId = customerToReturn.Id
            },
                                  customerToReturn));
        }
コード例 #14
0
        public async Task <IActionResult> AddCustomer([FromBody] CustomerForCreationDto customerForCreationDto)
        {
            if (customerForCreationDto == null)
            {
                return(BadRequest());
            }

            var customer = _mapper.Map <Customer>(customerForCreationDto);

            _repository.AddCustomer(customer);
            await _unitOfWork.CompleteAsync();

            var customerToReturn = _mapper.Map <CustomerDto>(customer);

            return(CreatedAtRoute("GetCustomer", new { id = customerToReturn.Id }, customerToReturn));
        }
コード例 #15
0
        public IActionResult PartiallyUpdateCustomer([FromRoute] int customerId,
                                                     [FromBody] CustomerForCreationDto patchCustomer)
        {
            if (patchCustomer == null)
            {
                return(BadRequest());
            }

            var CustomerEntity = _customerInfoRepository.GetCustomer(customerId);

            if (CustomerEntity == null)
            {
                return(StatusCode(500, "The customer you requested is not in the database"));
            }

            if (patchCustomer.Name != null)
            {
                if (patchCustomer.Name.Length > 50)
                {
                    return(StatusCode(500, "THe name is too long"));
                }
                CustomerEntity.Name = patchCustomer.Name;
            }

            if (patchCustomer.Adress != null)
            {
                CustomerEntity.Adress = patchCustomer.Adress;
            }

            if (patchCustomer.CreatedAtDate != null)
            {
                CustomerEntity.CreatedAtDate = patchCustomer.CreatedAtDate;
            }



            CustomerEntity.UpdatedAtDate = DateTime.Now;


            if (!_customerInfoRepository.Save())
            {
                return(StatusCode(500, "SOmething happened while handling your request"));
            }


            return(Ok(CustomerEntity));
        }
コード例 #16
0
        public IActionResult CreateCustomer([FromBody] CustomerForCreationDto customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var customerEntity = Mapper.Map <TalaCustomer>(customer);

            _talaCustomerRepository.Add(customerEntity);
            if (!_talaCustomerRepository.Save())
            {
                throw new Exception("Creating a talaCustomer failed on save");
            }

            return(NoContent());
        }
コード例 #17
0
        public async Task <IActionResult> AddCustomer(CustomerForCreationDto customerForCreation)
        {
            var creator = await _userRepo.GetUser(int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value));

            var customer = _mapper.Map <Customer>(customerForCreation);

            customer.User = creator;

            _repo.Add(customer);

            if (await _repo.SaveAll())
            {
                var customerToReturn = _mapper.Map <CustomerForReturnDto>(customer);
                return(CreatedAtRoute("GetCustomer", new { id = customer.Id }, customerToReturn));
            }

            throw new Exception("Creation of customer failed on save");
        }
コード例 #18
0
        public IActionResult CreateCustomer([FromBody] CustomerForCreationDto customerInfo)
        {
            var      customerError = "Please look at your data and make sure it's not empty, incorrect, or has values that are the same!";
            Customer customer      = new Customer();

            if (customerInfo == null)
            {
                return(BadRequest(customerError));
            }

            if (customerInfo.Name == customerInfo.Adress || customerInfo.Name == customerInfo.MobileNr || customerInfo.MobileNr == customerInfo.Adress)
            {
                return(BadRequest(customerError));
            }

            // Convert CustomerForCreationDto to Customer entity
            customer.Adress = customerInfo.Adress;
            if (customer.Adress.Length > 50)
            {
                return(StatusCode(500, "Your adress is too long. Please make sure it's less than 50 characters!"));
            }

            customer.Name = customerInfo.Name;
            if (customer.Name.Length > 50)
            {
                return(StatusCode(500, "Your name is too long. Please ensure it's less than 50 characters long!"));
            }



            customer.CreatedAtDate = DateTime.Now;
            customer.UpdatedAtDate = customer.CreatedAtDate;



            _customerInfoRepository.AddCustomer(customer);

            if (!_customerInfoRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            return(Ok(customer));
        }
コード例 #19
0
        public IActionResult AddCustomer([FromBody] CustomerForCreationDto customer)
        {
            if (customer == null)
            {
                return(BadRequest());
            }

            var customerForPost = Mapper.Map <Customer>(customer);

            _shopRepository.AddCustomer(customerForPost);


            if (!_shopRepository.Save())
            {
                return(StatusCode(500, "An internal server error occured. Please try again!"));
            }

            return(StatusCode(201));
        }
コード例 #20
0
        public ActionResult UpdateCustomer(Guid customerid, [FromBody] CustomerForCreationDto customer)
        {
            if (customer == null)
            {
                _logger.LogWarning("An invalid customer object was received for update");
                return(BadRequest());
            }
            else
            {
                _logger.LogInformation($"Starting to update customer {customerid}");
            }

            var customerFromRepo = _customerRepository.GetCustomerById(customerid);

            if (customerFromRepo == null)
            {
                _logger.LogInformation($"Customer {customerid} was not found to update");
                return(NotFound());
            }

            AutoMapper.Mapper.Map(customer, customerFromRepo);

            if (!ModelState.IsValid)
            {
                _logger.LogInformation("Updating new customer failed validation");
                return(BadRequest(ModelState));
            }

            _customerRepository.UpdateCustomer(customerFromRepo);

            if (!_customerRepository.Save())
            {
                throw new Exception("An unexpected error occured when updating the customer");
            }

            _logger.LogInformation($"Customer {customerid} successfully updated");
            return(NoContent());
        }
コード例 #21
0
        public IActionResult CreateCustomer([FromBody] CustomerForCreationDto customer)
        {
            if (customer == null)
            {
                return(BadRequest());
            }

            var customerEntity = Mapper.Map <Customer>(customer);

            _hotMealRepository.AddCustomer(customerEntity);

            if (!_hotMealRepository.Save())
            {
                throw new Exception("Creating an customer failed on save.");
                // return StatusCode(500, "A problem happened with handling your request.");
            }

            var customerToReturn = Mapper.Map <CustomerDto>(customerEntity);

            return(CreatedAtRoute("GetCustomer",
                                  new { id = customerToReturn.Id },
                                  customerToReturn));
        }
コード例 #22
0
        public async Task <IActionResult> CreateCustomer([FromBody] CustomerForCreationDto customerToCreate)
        {
            var customerEntity = new Customer()
            {
                Name        = customerToCreate.Name,
                LastName    = customerToCreate.LastName,
                Age         = customerToCreate.Age,
                Email       = customerToCreate.Email,
                Gender      = customerToCreate.Gender,
                PhoneNumber = customerToCreate.PhoneNumber,
                Address     = new Address()
                {
                    CountryId = customerToCreate.CountryId,
                    City      = customerToCreate.City,
                    Street    = customerToCreate.Street,
                    ZipCode   = customerToCreate.ZipCode
                }
            };

            this._repository.Customer.CreateCustomer(customerEntity);
            await this._repository.SaveAsync();

            return(CreatedAtRoute("GetCustomerById", new { id = customerEntity.Id }, customerEntity));
        }
コード例 #23
0
        public Customer AddCustomer(CustomerForCreationDto userForCreation)
        {
            var newCustomer = Mapper.Map <Customer>(userForCreation);

            return(_customerRepository.Create(newCustomer));
        }