using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Script.Services; using System.Web.Services; using Common; /// /// Summary description for UpdateCustomer /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService] public class UpdateCustomer : WebService { [WebMethod] public string NewInventoryItem(int customerId, string item, int category, int size) { HttpContext.Current.Trace.Warn("customerId", customerId.ToString()); HttpContext.Current.Trace.Warn("item", item); HttpContext.Current.Trace.Warn("category", category.ToString()); HttpContext.Current.Trace.Warn("size", size.ToString()); if (item == null || string.IsNullOrEmpty(item)) return "Missing item"; using (var db = new DataModel()) { if (db.InventoryItemTypes.Any(a => a.ItemType == item && a.InventoryItemTypeCategoryId == category && a.InventoryItemTypeSizeId == size)) return "Inventory Item already exists"; db.InventoryItemTypes.Add(new InventoryItemType { ItemType = item, InventoryItemTypeCategoryId = category, InventoryItemTypeSizeId = size }); db.SaveChanges(); return string.Empty; } } [WebMethod] public string NewLogin(int customerId, string emailAddress, string password) { HttpContext.Current.Trace.Warn("customerId", customerId.ToString()); HttpContext.Current.Trace.Warn("emailAddress", emailAddress); HttpContext.Current.Trace.Warn("password", password); if (emailAddress == null || string.IsNullOrEmpty(emailAddress)) return "Missing email address"; if (password == null || string.IsNullOrEmpty(password)) return "Missing password"; emailAddress = emailAddress.Trim().ToLower(); using (var db = new DataModel()) { var customer = db.Customers.FirstOrDefault(f => f.CustomerId == customerId); if (customer == null) return "Customer not found"; if (db.CustomerLogins.Any(a => a.EmailAddress == emailAddress)) return "Duplicate email address found"; var guid = Guid.NewGuid(); db.CustomerLogins.Add(new CustomerLogin { GUID = guid, CustomerId = customerId, EmailAddress = emailAddress, Password = password, IsActive = true, IsLockedOut = false }); db.SaveChanges(); } return string.Empty; } [WebMethod] public bool UpdateInventory(int customerId, int pk, string name, string value) { if (!HttpContext.Current.User.Identity.IsAuthenticated) return false; HttpContext.Current.Trace.Warn("Name", HttpContext.Current.User.Identity.Name); HttpContext.Current.Trace.Warn("customerId", customerId.ToString()); HttpContext.Current.Trace.Warn("pk", pk.ToString()); HttpContext.Current.Trace.Warn("name", name); HttpContext.Current.Trace.Warn("value", value); int cost; if (name == null || value == null || !int.TryParse(value, out cost)) return false; using (var db = new DataModel()) { var operatorLogin = db.OperatorLogins.FirstOrDefault(f => f.Username == HttpContext.Current.User.Identity.Name); if (operatorLogin == null) return false; var inventory = db.CustomerInventories.FirstOrDefault(f => f.CustomerId == customerId && f.InventoryItemTypeId == pk); if (inventory == null) { inventory = new CustomerInventory { CustomerId = customerId, InventoryItemTypeId = pk, OperatorLoginId = operatorLogin.OperatorLoginId, TimeStamp = DateTime.Now }; switch (name) { case "CostGeneralWash": inventory.CostGeneralWash = cost; break; case "CostRepeatWash": inventory.CostRepeatWash = cost; break; case "CostStainWash": inventory.CostStainWash = cost; break; } db.CustomerInventories.Add(inventory); db.SaveChanges(); return true; } switch (name) { case "CostGeneralWash": inventory.CostGeneralWash = cost; break; case "CostRepeatWash": inventory.CostRepeatWash = cost; break; case "CostStainWash": inventory.CostStainWash = cost; break; default: return false; } db.SaveChanges(); return true; } } [WebMethod] public bool UpdateLogin(int pk, string name, string value) { using (var db = new DataModel()) { var login = db.CustomerLogins.FirstOrDefault(f => f.CustomerLoginId == pk); if (login == null) return false; bool b; switch (name) { case "EmailAddress": login.EmailAddress = value; break; case "Password": login.Password = value; break; case "IsActive": if (bool.TryParse(value, out b)) login.IsActive = b; break; case "IsLockedOut": if (bool.TryParse(value, out b)) login.IsLockedOut = b; break; default: return false; } db.SaveChanges(); } return true; } }