public IActionResult PutCustomer(int id, CustomerControllerModel customer)
        {
            try
            {
                _log.Info("Reached PutCustomer(int id, CustomerControllerModel customer) in CustomersController.cs");

                if (!ModelState.IsValid)
                {
                    _log.Error("A ModelState isn't valid error occured in PutCustomer(int id, CustomerControllerModel customer) in CustomersController.cs");
                    return(BadRequest()); // NotFound
                }

                if (id != customer.Id)
                {
                    _log.Error("Customer object isn't matched with given id! Error occured in PutCustomer(int id, CustomerControllerModel customer) in CustomersController.cs");
                    return(NotFound());
                }
                bool exists = _service.Update(_mapper.Map <CustomerServiceModel>(customer));

                if (exists)
                {
                    _log.Info("Modified Customer object in PutCustomer(int id, CustomerControllerModel customer) in CustomersController.cs");
                    return(Ok(customer));
                }

                _log.Error("Customer object with given id doesn't exist! Error occured in PutCustomer(int id, CustomerControllerModel customer) in CustomersController.cs");

                return(NotFound());
            }
            catch (Exception e)
            {
                _log.Error(string.Format("An exception {0} occured in PutCustomer(int id, CustomerControllerModel customer) in CustomersController.cs", e));
                return(StatusCode(500));
            }
        }
        public IActionResult GetCustomer(int id)
        {
            try
            {
                _log.Info("Reached GetCustomer(int id) in CustomersController.cs");


                CustomerControllerModel customer = _mapper.Map <CustomerControllerModel>(_service.Get(id));

                if (customer == null)
                {
                    _log.Error("Got null object in GetCustomer(int id) in CustomersController.cs");
                    return(NotFound());
                }

                _log.Info("Returned Customer object from GetCustomer(int id) in CustomersController.cs");

                return(Ok(customer));
            }
            catch (Exception e)
            {
                _log.Error(string.Format("An exception {0} occured in GetCustomer(int id) in CustomersController.cs", e));
                return(StatusCode(500)); //  internalError
            }
        }
Example #3
0
        private void SetupModels()
        {
            _customerId = 1;

            _customerServiceModel = new CustomerServiceModel {
                Id = _customerId, Name = "pera", LastName = "Peric"
            };
            _customerServiceModelInvalid = new CustomerServiceModel {
                Id = _customerId, Name = "pera"
            };
            _customerControllerModelInvalid = new CustomerControllerModel
            {
                Id = _customerServiceModelInvalid.Id, Name = _customerServiceModelInvalid.Name
            };
            _customerControllerModel = new CustomerControllerModel
            {
                Id       = _customerServiceModel.Id,
                Name     = _customerServiceModel.Name,
                LastName = _customerServiceModel.LastName
            };

            _custumerSreviceModelList = new List <CustomerServiceModel>
            {
                new CustomerServiceModel  {
                    Id = 2, Name = "pera", LastName = "Peric"
                },
                new CustomerServiceModel  {
                    Id = 3, Name = "pera", LastName = "Peric"
                }
            };

            _custumerControllerModelList = new List <CustomerControllerModel>
            {
                new CustomerControllerModel  {
                    Id = 2, Name = "pera", LastName = "Peric"
                },
                new CustomerControllerModel  {
                    Id = 3, Name = "pera", LastName = "Peric"
                }
            };
        }
        public IActionResult PostCustomer([FromBody] CustomerControllerModel customer)
        {
            try
            {
                _log.Info("Reached PostCustomer([FromBody] CustomerControllerModel customer) in CustomersController.cs");

                if (!ModelState.IsValid)
                {
                    _log.Error("A ModelState isn't valid error occured in PostCustomer([FromBody] CustomerControllerModel customer) in CustomersController.cs");
                    return(StatusCode(400));
                }

                _service.Add(_mapper.Map <CustomerServiceModel>(customer));
                _log.Info("Added new Customer object in PostCustomer([FromBody] CustomerControllerModel customer) in CustomersController.cs");

                return(Ok(customer));
            }
            catch (Exception e)
            {
                _log.Error(string.Format("An exception {0} occured in PostCustomer([FromBody] CustomerControllerModel customer) in CustomersController.cs", e));
                return(StatusCode(500));
            }
        }