public void UpdateOrder(Order oldOrder, Order newOrder) { using (var db = new HotelServicesDbContext()) { if (oldOrder == null || newOrder == null) { return; } var ord = db.Orders.FirstOrDefault(o => o.Id == oldOrder.Id); if (ord == null) { return; } ord.Id = newOrder.Id; ord.Cost = newOrder.Cost; ord.IsPaid = newOrder.IsPaid; ord.OrderDate = newOrder.OrderDate; ord.Service = db.ServiceInfos.FirstOrDefault(s => s.Id == newOrder.Service.Id); ord.Units = newOrder.Units; ord.User = db.Users.FirstOrDefault(u => u.Id == newOrder.User.Id); db.SaveChanges(); } }
public static void AddUserToDb(string fio, string login, string password, string roleName) { if (fio.IsNullOrEmpty() || login.IsNullOrEmpty() || password.IsNullOrEmpty() || roleName.IsNullOrEmpty()) { return; } using (var db = new HotelServicesDbContext()) { var role = db.Roles.FirstOrDefault( r => string.Equals(r.Name, roleName, StringComparison.CurrentCultureIgnoreCase)); if (role == null) { return; } var user = new User { Id = Guid.NewGuid().ToString(), Fio = fio, Login = login, Password = password, Role = role }; db.Users.Add(user); db.SaveChanges(); } }
public void RemoveUser(User user) { if (user == null) { return; } using (var db = new HotelServicesDbContext()) { db.Users.Remove(user); db.SaveChanges(); } }
public static void ClearOldEntities(string userId = null, string sessionId = null) { if (userId == null && sessionId == null) { return; } if (userId != null) { using (var db = new HotelServicesDbContext()) { var a = (from auth in db.Authorizations join u in db.Users on auth.User.Id equals u.Id where auth.User.Id == userId select new Authorization { Id = auth.Id, User = u, SessionId = auth.SessionId }).ToList(); if (a.Count <= 0) { return; } db.Authorizations.RemoveRange(a); db.SaveChanges(); } return; } using (var db = new HotelServicesDbContext()) { var a = (from auth in db.Authorizations join u in db.Users on auth.User.Id equals u.Id where auth.SessionId == sessionId select new Authorization { Id = auth.Id, User = u, SessionId = auth.SessionId }).ToList(); if (a.Count <= 0) { return; } db.Authorizations.RemoveRange(a); db.SaveChanges(); } }
public void AddUser(User user) { if (user == null) { return; } using (var db = new HotelServicesDbContext()) { user.Role = db.Roles.FirstOrDefault(r => r.Id == user.Role.Id); db.Users.Add(user); db.SaveChanges(); } }
public void RemoveOrder(Order order) { if (order == null) { return; } using (var db = new HotelServicesDbContext()) { db.Orders.Remove(order); db.SaveChanges(); } }
public void RemoveServiceInfo(ServiceInfo service) { if (service == null) { return; } using (var db = new HotelServicesDbContext()) { db.ServiceInfos.Remove(service); db.SaveChanges(); } }
public static void AuthorizeInDb(string sessionId, string userId) { ClearOldEntities(userId); using (var db = new HotelServicesDbContext()) { var a = new Authorization { Id = Guid.NewGuid().ToString(), SessionId = sessionId, User = db.Users.FirstOrDefault(u => u.Id == userId) }; db.Authorizations.Add(a); db.SaveChanges(); } }
public void AddOrder(Order order) { if (order == null) { return; } using (var db = new HotelServicesDbContext()) { order.User.Role = db.Roles.FirstOrDefault(r => r.Id == order.User.Role.Id); order.User = db.Users.FirstOrDefault(u => u.Id == order.User.Id); order.Service = db.ServiceInfos.FirstOrDefault(s => s.Id == order.Service.Id); db.Orders.Add(order); db.SaveChanges(); } }
public void UpdateService(ServiceInfo oldService, ServiceInfo newService) { using (var db = new HotelServicesDbContext()) { if (oldService == null || newService == null) { return; } var ord = db.ServiceInfos.FirstOrDefault(o => o.Id == oldService.Id); if (ord == null) { return; } ord.IsDeprecated = true; db.ServiceInfos.Add(newService); db.SaveChanges(); } }