コード例 #1
0
        public void SaveCustomer(Customer customer)
        {
            // Preparare validation return data
            ICollection<ValidationResult> validationResults;

            // Try to validate given data
            if(customer.Validate(out validationResults))
            {
                // If a new customer should be created
                if(customer.CustomerId == 0)
                {
                    CustomerDAL.InsertCustomer(customer);
                }
                // Existing customer should be updated
                else
                {
                    // Check that the customer exists before update
                    if(CustomerDAL.GetCustomerById(customer.CustomerId) == null)
                    {
                        throw new ApplicationException(String.Format("Kunden {0} som skulle uppdateras är tyvärr borttagen.", customer.Name));
                    }

                    // Update existing customer
                    CustomerDAL.UpdateCustomer(customer);
                }
            }
            // Validation failed
            else
            {
                // Create exception
                ApplicationException exception = new ApplicationException("Kundobjektet innehöll felaktiga värden. Var god försök igen.");
                
                // Add validation data to exception.
                exception.Data.Add("ValidationResults", validationResults);

                throw exception;
            }
        }
コード例 #2
0
        public void UpdateCustomer(Customer customer)
        {
            // Create connection object
            using (this.CreateConnection())
            {
                try
                {
                    SqlCommand cmd;

                    // Connect to database
                    cmd = this.Setup("appSchema.usp_CustomerUpdate", DALOptions.closedConnection);

                    // Add in parameters for Stored procedure
                    cmd.Parameters.Add("@CustomerId", SqlDbType.Int).Value = customer.CustomerId;
                    cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = customer.Name;
                    cmd.Parameters.Add("@Address", SqlDbType.VarChar, 40).Value = customer.Address;
                    cmd.Parameters.Add("@PostNumber", SqlDbType.VarChar, 6).Value = customer.PostNumber;
                    cmd.Parameters.Add("@City", SqlDbType.VarChar, 30).Value = customer.City;
                    cmd.Parameters.Add("@EmailAddress", SqlDbType.VarChar, 50).Value = customer.EmailAddress;
                    cmd.Parameters.Add("@PhoneNumber", SqlDbType.VarChar, 20).Value = customer.PhoneNumber;
                    cmd.Parameters.Add("@CellPhoneNumber", SqlDbType.VarChar, 20).Value = customer.CellPhoneNumber;
                    cmd.Parameters.Add("@ParentCustomerId", SqlDbType.Int).Value = customer.ParentCustomerId;
                    cmd.Parameters.Add("@ImageSrc", SqlDbType.VarChar, 50).Value = customer.ImageSrc;
                    cmd.Parameters.Add("@Notes", SqlDbType.VarChar, 200).Value = customer.Notes;

                    // Open DB connection
                    connection.Open();

                    // Execute insert to database
                    cmd.ExecuteNonQuery();
                }
                catch
                {
                    // Throw exception
                    throw new ApplicationException(DAL_ERROR_MSG);
                }
            }
        }
コード例 #3
0
        public IHttpActionResult Post(Customer customer)
        {
            // Check for bad values, done by the data annotations in the model class.
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            // Try to save customer
            try
            {
                customerService.SaveCustomer(customer);
            }
            catch (DataBaseEntryNotFoundException)
            {
                return NotFound();
            }
            catch (DuplicateNameException)
            {
                return Conflict();
            }
            catch (ApprovedException exception)
            {
                return BadRequest(exception.Message);
            }
            catch
            {
                return InternalServerError();
            }

            // Respond that the booking was created and redirect
            return Ok(customer); //CreatedAtRoute("DefaultApi", new { id = customer.CustomerId }, customer);
        }
コード例 #4
0
 // Methods
     public void DeleteCustomer(Customer customer)
     {
         DeleteCustomer(customer.CustomerId);
     }