示例#1
0
        public IHttpActionResult GenerateKey([FromBody] PostKeyRequest postKeyRequest)
        {
            if (postKeyRequest.Customer == null || postKeyRequest.Numbers == 0)
            {
                return(BadRequest());
            }

            // Searches the customer by Name in database
            Customer custm = db.Customers.FirstOrDefault(p => p.Name.Equals(postKeyRequest.Customer.Name, StringComparison.OrdinalIgnoreCase));

            if (custm == null)
            {
                // Adds a new customer to database
                custm = db.Customers.Add(new Customer {
                    Name = postKeyRequest.Customer.Name
                });

                // Saves adding a new customer to database
                db.SaveChanges();
            }

            for (int i = 0; i < postKeyRequest.Numbers; i++)
            {
                // Generates a key
                string key     = Helper.GetKey();
                bool   isFound = true;

                while (isFound)
                {
                    // Checks if exist key in database
                    isFound = Helper.IsExistsKey(key, db);

                    // If exists the key then generates a new unique key
                    if (isFound)
                    {
                        key = Helper.GetKey();
                    }
                }

                // Adds the key to database
                db.Keys.Add(new Key {
                    CustomerId = custm.Id, Value = key
                });
            }

            // Saves all changes to database
            db.SaveChanges();

            return(Ok());
        }
示例#2
0
        public void FixtureTearDown()
        {
            using (var context = new KeyContext())
            {
                foreach (var id in keys)
                {
                    var key = context.Keys.FirstOrDefault(k => k.ID == id);
                    context.Keys.Remove(key);
                    context.SaveChanges();

                    var keyAudits = context.KeyAudits.Where(ke => ke.ID == id);
                    context.KeyAudits.RemoveRange(keyAudits);
                    context.SaveChanges();
                }
            }
        }
示例#3
0
 public ActionResult Delete(int id, Key deleteKey)
 {
     try
     {
         using (var context = new KeyContext())
         {
             Key key = context.Keys.FirstOrDefault(k => k.ID == id);
             context.Keys.Remove(key);
             context.SaveChanges();
         }
         return(RedirectToAction("Index"));
     }
     catch (Exception e)
     {
         return(View("Error", e));
     }
 }
示例#4
0
        public ActionResult Create(int?id, Key newKey)
        {
            try
            {
                newKey.Updated = DateTime.Now;

                using (var context = new KeyContext())
                {
                    context.Keys.Add(newKey);
                    context.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                return(View("Error", e));
            }
        }
示例#5
0
        public ActionResult Edit(int id, Key editKey)
        {
            try
            {
                editKey.Updated = DateTime.Now;

                using (var context = new KeyContext())
                {
                    context.Entry(editKey).State = EntityState.Modified;
                    context.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                return(View("Error", e));
            }
        }