Exemplo n.º 1
0
        public static AuthenticationDTO AuthenticateCustomer(string username, string password)
        {
            //declare a dto object
            AuthenticationDTO dto = null;

            //creating the session to the database (called a context or DbContext)
            var db = new MarinaEntities();

            //find the authentication object based on credentials passed in
            var auth = db.Customers.
                       SingleOrDefault(a => a.FirstName == username && a.LastName == password); // pseudo login

            if (auth != null)
            {
                //authentication passed, so instantiate the dto object
                dto = new AuthenticationDTO
                {
                    Id        = auth.ID.ToString(),
                    FirstName = auth.FirstName,
                    LastName  = auth.LastName,
                    Phone     = auth.Phone,
                    City      = auth.City,
                    Username  = $"{auth.FirstName}",                      // pseudo login
                    Password  = $"{auth.LastName}"                        // pseudo login
                };
            }
            //return the dto object
            return(dto);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Static method handles adding a new authentication object to the DB
        /// </summary>
        /// <param name="auth">The authentication object is a DTO object</param>
        public static void AddAuthentication(AuthenticationDTO auth)
        {
            //use the context to add a new authentication object
            var db = new MarinaEntities();

            //assign from dto object to the entity object
            var authFromContext = new Customer
            {
                FirstName = auth.FirstName,
                LastName  = auth.LastName,
                Phone     = auth.Phone,
                City      = auth.City
            };

            db.Customers.Add(authFromContext);
            db.SaveChanges();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Static method handles the update of an existing authentication object
        /// </summary>
        /// <param name="auth">The authentication object as a DTO</param>
        public static void UpdateAuthentication(AuthenticationDTO auth)
        {
            //use the context to update an existing record
            var id = Convert.ToInt32(auth.Id);
            var db = new MarinaEntities();

            //we need to get an authentication object from the context
            //so that we can update it with values from the dto passed in
            var authFromContext = db.Customers.SingleOrDefault(a => a.ID == id);

            authFromContext.FirstName = auth.FirstName;
            authFromContext.Phone     = auth.Phone;
            authFromContext.City      = auth.City;

            //save changes--no need to add the object back to the context as the context
            //already has it
            db.SaveChanges();
        }
Exemplo n.º 4
0
        public static AuthenticationDTO FindAuthentication(int customerId)
        {
            //use the context to find the authentication object based on customer id
            var db = new MarinaEntities();

            var auth = db.Customers.
                       SingleOrDefault(a => a.ID == customerId);
            var dto = new AuthenticationDTO
            {
                Id        = auth.ID.ToString(),
                FirstName = auth.FirstName,
                LastName  = auth.LastName,
                Phone     = auth.Phone,
                City      = auth.City,
                Username  = $"{auth.FirstName}",                      // pseudo login
                Password  = $"{auth.LastName}"                        // pseudo login
            };

            return(dto);
        }