public ActionResult AddSubscription(GymSubscription gymSubscription) { gymSubscription.UserId = User.Identity.GetUserId(); manager.SaveSubscription(gymSubscription); return(RedirectToAction(nameof(AddSubscription))); }
void IFitAppManager.SaveSubscription(GymSubscription gymSubscription) { var store = new UserStore <FitAppUser>(Context); var userManager = new UserManager <FitAppUser>(store); gymSubscription.Client = userManager.FindById(gymSubscription.UserId); Context.GymSubscriptions.Add(gymSubscription); Context.Entry(gymSubscription.Client).State = EntityState.Unchanged; Context.SaveChanges(); }
public bool AddGym(ulong userId, string gymName) { _logger.Trace($"SubscriptionManager::AddGym [UserId={userId}, GymName={gymName}]"); using (var conn = DataAccessLayer.CreateFactory().Open()) { var subscription = GetUserSubscriptions(userId); //Gym exists var gymSub = subscription.Gyms.FirstOrDefault(x => string.Compare(x.Name, gymName, true) == 0); if (gymSub == null) { //Create new gym subscription object. gymSub = new GymSubscription { UserId = userId, Name = gymName }; subscription.Gyms.Add(gymSub); } else { //Already exists. return(true); } try { var result = conn.Save(subscription, true); if (result) { _logger.Debug($"Gym Added!"); } else { _logger.Debug("Gym Updated!"); } return(true); } catch (MySql.Data.MySqlClient.MySqlException) { return(AddGym(userId, gymName)); } catch (Exception ex) { _logger.Error(ex); return(false); } } }