Inheritance: DbContext
Exemplo n.º 1
0
 // GET /Tasks
 /// <summary>
 /// Retrieves all the tasks
 /// </summary>
 /// <returns>All the tasks</returns>
 public IEnumerable<Task> Get()
 {
     using (var dataContext = new DataContext())
     {
         var data = dataContext.Tasks.AsNoTracking();
         var model = data.Where(x => x.User.UserName == this.User.Identity.Name).ToList();
         return model;
     }
 }
Exemplo n.º 2
0
 // GET /Tasks/5
 /// <summary>
 /// Gets task with the specified id.
 /// </summary>
 /// <param name="id">The id.</param>
 /// <returns></returns>
 public Task Get(int id)
 {
     using (var dataContext = new DataContext())
     {
         var data = dataContext.Tasks.AsNoTracking();
         var model = data.FirstOrDefault(x => x.Id == id && x.User.UserName == this.User.Identity.Name);
         return model;
     }
 }
Exemplo n.º 3
0
 public ActionResult Index(int? id)
 {
     using (var dataContext = new DataContext())
     {
         var data = dataContext.Tasks.AsNoTracking();
         var model = id != null ? (object)data.FirstOrDefault(x => x.Id == id && x.User.UserName == User.Identity.Name) : data.Where(x => x.User.UserName == User.Identity.Name).ToList();
         return Json(model, JsonRequestBehavior.AllowGet);
     }
 }
Exemplo n.º 4
0
 // DELETE api/<controller>/5
 /// <summary>
 /// Deletes the specified id.
 /// </summary>
 /// <param name="id">The id.</param>
 public void Delete(int id)
 {
     using (var dataContext = new DataContext())
     {
         var item = dataContext.Tasks.FirstOrDefault(x => x.Id == id && x.User.UserName == this.User.Identity.Name);
         if (item == null) return;
         dataContext.Tasks.Remove(item);
         dataContext.SaveChanges();
     }
 }
Exemplo n.º 5
0
        public ActionResult Index(int id, TaskModel model)
        {
            using (var dataContext = new DataContext())
            {
                var item = dataContext.Tasks.FirstOrDefault(x => x.Id == model.Id && x.User.UserName == User.Identity.Name);
                if (item == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound);

                item.Title = model.Title;
                item.Completed = model.Completed;
                dataContext.SaveChanges();
                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }
        }
Exemplo n.º 6
0
 public ActionResult Index(int id)
 {
     using (var dataContext = new DataContext())
     {
         var item = dataContext.Tasks.FirstOrDefault(x => x.Id == id && x.User.UserName == User.Identity.Name);
         if (item != null)
         {
             dataContext.Tasks.Remove(item);
             dataContext.SaveChanges();
         }
         return new HttpStatusCodeResult(HttpStatusCode.OK);
     }
 }
Exemplo n.º 7
0
 // POST /Tasks
 /// <summary>
 /// Posts the specified model.
 /// </summary>
 /// <param name="model">The model.</param>
 public void Post(TaskModel model)
 {
     using (var dataContext = new DataContext())
     {
         var item = new Task
         {
             Title = model.Title,
             Completed = model.Completed,
             User = dataContext.UserProfiles.AsNoTracking().First(x => x.UserName == this.User.Identity.Name)
         };
         dataContext.Tasks.Add(item);
         dataContext.SaveChanges();
     }
 }
Exemplo n.º 8
0
 public ActionResult Index(TaskModel model)
 {
     using (var dataContext = new DataContext())
     {
         var item = new Task
         {
             Title = model.Title,
             Completed = model.Completed,
             User = dataContext.UserProfiles.AsNoTracking().First(x => x.UserName == User.Identity.Name)
         };
         dataContext.Tasks.Add(item);
         dataContext.SaveChanges();
         return Json(item, JsonRequestBehavior.AllowGet);
     }
 }
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<DataContext>(null);

                try
                {
                    using (var context = new DataContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    if(!WebSecurity.Initialized)
                        WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
Exemplo n.º 10
0
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (DataContext db = new DataContext())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Exemplo n.º 11
0
        // PUT api/<controller>/5
        /// <summary>
        /// Puts the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="model">The model.</param>
        /// <exception cref="System.Web.Http.HttpResponseException"></exception>
        public void Put(int id, TaskModel model)
        {
            using (var dataContext = new DataContext())
            {
                var item = dataContext.Tasks.FirstOrDefault(x => x.Id == model.Id && x.User.UserName == this.User.Identity.Name);
                if (item == null) throw new HttpResponseException(HttpStatusCode.NotFound);

                item.Title = model.Title;
                item.Completed = model.Completed;
                dataContext.SaveChanges();
            }
        }