Ejemplo n.º 1
0
        /// <summary>
        /// Retries the customers data from the database.
        /// </summary>
        /// <returns>Collection of the customers.</returns>
        public async Task <List <Customer> > GetCustomers()
        {
            try
            {
                using (CustomersContext context = new CustomersContext())
                {
                    List <CustomerDb> customersDb = await context.CustomerDbs.ToListAsync();

                    List <Customer> customers = customersDb
                                                .Select(cdb =>
                    {
                        Customer c = new Customer()
                        {
                            Id             = cdb.Id,
                            AddressLineOne = cdb.AddressLineOne,
                            Category       = cdb.Category,
                            Country        = cdb.Country,
                            DateOfBirth    = cdb.DateOfBirth,
                            Gender         = cdb.Gender,
                            HouseNumber    = cdb.HouseNumber,
                            Name           = cdb.Name,
                            State          = cdb.State,
                        };
                        return(c);
                    }
                                                        ).ToList();

                    return(customers);
                }
            }
            catch (InvalidCastException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes the customer from the database.
        /// </summary>
        /// <param name="customer">The customer to be removed.</param>
        /// <returns>If successful returns datetime of the remove operation, otherwise null.</returns>
        public async Task <Nullable <DateTime> > RemoveCustomer(Customer customer)
        {
            try
            {
                if (customer == null)
                {
                    throw new ArgumentNullException("Null Customer cannot be updated.");
                }
                using (CustomersContext context = new CustomersContext())
                {
                    CustomerDb customerDb = context.CustomerDbs.FirstOrDefault(c => c.Id == customer.Id);

                    if (customerDb == null)
                    {
                        return(null);
                    }

                    context.CustomerDbs.Remove(customerDb);

                    await context.SaveChangesAsync();
                    await EnterModificationHistory(context, customer, ModificationType.Remove);

                    this.lastDataBaseModificationTime = DateTime.Now;

                    return(this.lastDataBaseModificationTime);
                }
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (NullReferenceException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds the customer to the database.
        /// </summary>
        /// <param name="customer">The customer to be added.</param>
        /// <param name="cancellationToken">Cancellation token to cancel save.</param>
        /// <returns>If successful returns datetime of the add operation, otherwise null.</returns>
        public async Task <Nullable <DateTime> > AddCustomer(Customer customer, CancellationToken cancellationToken = new CancellationToken())
        {
            try
            {
                if (customer == null)
                {
                    throw new ArgumentNullException("Null Customer cannot be added");
                }

                if (!customer.IsValid)
                {
                    return(null);
                }

                using (CustomersContext context = new CustomersContext())
                {
                    CustomerDb customerDb = new CustomerDb()
                    {
                        AddressLineOne = customer.AddressLineOne,
                        Category       = customer.Category,
                        Country        = customer.Country,
                        DateOfBirth    = customer.DateOfBirth,
                        Gender         = customer.Gender,
                        HouseNumber    = customer.HouseNumber,
                        Name           = customer.Name,
                        State          = customer.State
                    };

                    context.CustomerDbs.Add(customerDb);

                    await context.SaveChangesAsync(cancellationToken);

                    if (!cancellationToken.IsCancellationRequested)
                    {
                        // The Id is created by the database so before
                        // logging modification history entry update the customer with the new Id.
                        customer.Id = customerDb.Id;
                        await EnterModificationHistory(context, customer, ModificationType.Add);

                        this.lastDataBaseModificationTime = DateTime.Now;
                        return(this.lastDataBaseModificationTime);
                    }
                    return(null);
                }
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (NullReferenceException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception)
            {
                // Cancelletion exception can be wrapped in several other exceptions.
                // Instead of unwrapping it check if cancellation requested.
                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }
                throw;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Updates the customer in the database
        /// </summary>
        /// <param name="customer">The customer to be updated.</param>
        /// <param name="cancellationToken">Cancellation token to cancel save.</param>
        /// <returns>If successful returns datetime of the update operation, otherwise null.</returns>
        public async Task <Nullable <DateTime> > UpdateCustomer(Customer customer, CancellationToken cancellationToken = new CancellationToken())
        {
            try
            {
                if (customer == null)
                {
                    throw new ArgumentNullException("Null Customer cannot be updated.");
                }
                if (!customer.IsValid)
                {
                    return(null);
                }
                using (CustomersContext context = new CustomersContext())
                {
                    CustomerDb customerDb = context.CustomerDbs.Find(customer.Id);

                    if (customerDb == null)
                    {
                        return(null);
                    }

                    customerDb.AddressLineOne = customer.AddressLineOne;
                    customerDb.Category       = customer.Category;
                    customerDb.Country        = customer.Country;
                    customerDb.DateOfBirth    = customer.DateOfBirth;
                    customerDb.Gender         = customer.Gender;
                    customerDb.HouseNumber    = customer.HouseNumber;
                    customerDb.Name           = customer.Name;
                    customerDb.State          = customer.State;

                    await context.SaveChangesAsync(cancellationToken);

                    if (!cancellationToken.IsCancellationRequested)
                    {
                        await EnterModificationHistory(context, customer, ModificationType.Update);

                        this.lastDataBaseModificationTime = DateTime.Now;
                        return(this.lastDataBaseModificationTime);
                    }
                    return(null);
                }
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (NullReferenceException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception)
            {
                // Cancelletion exception can be wrapped in several other exceptions.
                // Instead of unwrapping it check if cancellation requested.
                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }
                throw;
            }
        }