Пример #1
0
 public static Task UpdateUserAsync(AuthenticationResult result)
 {
     var fb = GetUserFacebookClient();
     return Task.Run(() =>
     {
         Logger.InfoFormat("Performing update of user {0}", result.UserName);
         try
         {
             using (var db = new ShoppingListDbContext())
             {
                 UserProfile user =
                     db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == result.UserName.ToLower());
                 if (user != null)
                 {
                     dynamic picture = fb.Get("me", new { fields = "picture", type = "square" });
                     user.AccessToken = result.ExtraData.ReadField(Oauth.FieldNames.AccessToken);
                     user.Name = result.ExtraData.ReadField(Oauth.FieldNames.Name);
                     user.Gender = result.ExtraData.ReadField(Oauth.FieldNames.Gender);
                     user.PictureUrl = picture.picture.data.url;
                     db.SaveChanges();
                 }
             }
         }
         catch (Exception ex)
         {
             Logger.Error(String.Format("Update of user {0} failed.", result.UserName), ex);
         }
     });
 }
Пример #2
0
        private static string ReadAccessTokenFromDb()
        {
            var db   = new ShoppingListDbContext();
            var user = db.UserProfiles.FirstOrDefault(userProfile => userProfile.UserName.ToLower() == Thread.CurrentPrincipal.Identity.Name.ToLower());

            return(user != null ? user.AccessToken : null);
        }
Пример #3
0
        public ItemViewModel GetItemByID(int?id)
        {
            Item entity;

            using (var ctx = new ShoppingListDbContext())
            {
                entity = ctx.Items.SingleOrDefault(e => e.ID == id);
            }
            if (entity != null)
            {
                return new ItemViewModel
                       {
                           ID             = entity.ID,
                           ShoppingListID = entity.ShoppingListID,
                           Content        = entity.Content,
                           IsChecked      = entity.IsChecked,
                           Priority       = entity.Priority,
                           CreatedUtc     = entity.CreatedUtc,
                           ModifiedUtc    = entity.ModifiedUtc
                       }
            }
            ;
            else
            {
                return(null);
            }
        }
Пример #4
0
        public static Task UpdateUserAsync(AuthenticationResult result)
        {
            var fb = GetUserFacebookClient();

            return(Task.Run(() =>
            {
                Logger.InfoFormat("Performing update of user {0}", result.UserName);
                try
                {
                    using (var db = new ShoppingListDbContext())
                    {
                        UserProfile user =
                            db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == result.UserName.ToLower());
                        if (user != null)
                        {
                            dynamic picture = fb.Get("me", new { fields = "picture", type = "square" });
                            user.AccessToken = result.ExtraData.ReadField(Oauth.FieldNames.AccessToken);
                            user.Name = result.ExtraData.ReadField(Oauth.FieldNames.Name);
                            user.Gender = result.ExtraData.ReadField(Oauth.FieldNames.Gender);
                            user.PictureUrl = picture.picture.data.url;
                            db.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(String.Format("Update of user {0} failed.", result.UserName), ex);
                }
            }));
        }
Пример #5
0
 public string GetShoppingListName(int id)
 {
     using (var ctx = new ShoppingListDbContext())
     {
         return(ctx.ShoppingLists.Where(e => e.ID == id).SingleOrDefault().Name);
     }
 }
Пример #6
0
        public ShoppingListViewModel GetListByID(int?id)
        {
            ShoppingList entity;

            using (var ctx = new ShoppingListDbContext())
            {
                entity = ctx.ShoppingLists.SingleOrDefault(e => e.ID == id);
            }
            if (entity != null)
            {
                return new ShoppingListViewModel
                       {
                           ID          = entity.ID,
                           Name        = entity.Name,
                           Group       = entity.Group,
                           Color       = entity.Color,
                           CreatedUtc  = entity.CreatedUtc,
                           ModifiedUtc = entity.ModifieddUtc
                       }
            }
            ;
            else
            {
                return(null);
            }
        }
 public SqlShoppingListRepo(ShoppingListDbContext context,
                            GroceryPredictionPool predictionEnginePool, IUserPasswordSalt salt)
 {
     _context              = context;
     _waypointsRepo        = new SqlWaypointsRepo(_context);
     _predictionEnginePool = predictionEnginePool;
     _salt = salt;
 }
        public bool Delete(int itemId)
        {
            var dbContext = new ShoppingListDbContext();

            var itemSqlDatabase = new ItemSqlStorage(dbContext);

            return(new Item().DeleteItem(itemId, itemSqlDatabase));
        }
Пример #9
0
        public bool Delete(int shoppingItemId)
        {
            var dbContext = new ShoppingListDbContext();

            var shoppingItemSqlStorage = new ShoppingItemSqlStorage(dbContext);

            return(new ShoppingItem().DeleteItem(shoppingItemId, shoppingItemSqlStorage));
        }
        public bool Delete(int pictureId)
        {
            var dbContext = new ShoppingListDbContext();

            var pictureSqlStorage = new PictureSqlStorage(dbContext);

            return(new Picture().DeleteItem(pictureId, pictureSqlStorage));
        }
Пример #11
0
        public bool Edit(int itemId, string noteBody)
        {
            using (var ctx = new ShoppingListDbContext())
            {
                var command = @"UPDATE Note SET Body = {0}, ModifiedUtc = {1} WHERE ItemId = {2}";
                var result  = ctx.Database.ExecuteSqlCommand(command, noteBody, DateTimeOffset.UtcNow, itemId);

                return(result == 1);
            }
        }
Пример #12
0
        public bool Create(int itemId, string noteBody)
        {
            using (var ctx = new ShoppingListDbContext())
            {
                var command = @"INSERT INTO Note (ItemId, Body, CreatedUtc) VALUES({0}, {1}, {2})";
                var result  = ctx.Database.ExecuteSqlCommand(command, itemId, noteBody, DateTimeOffset.UtcNow);

                return(result == 1);
            }
        }
Пример #13
0
 public IEnumerable <ShoppingListViewModel> GetList()
 {
     using (var ctx = new ShoppingListDbContext())
     {
         return(ctx.ShoppingLists.Where(e => e.UserID == _userID).Select(
                    e => new ShoppingListViewModel {
             ID = e.ID, Name = e.Name, Color = e.Color, Group = e.Group, CreatedUtc = e.CreatedUtc, ModifiedUtc = e.ModifieddUtc
         }
                    ).ToArray());
     }
 }
Пример #14
0
        public Item AddUpdate(Item item)
        {
            var dbContext = new ShoppingListDbContext();

            var itemSqlDatabase = new ItemSqlStorage(dbContext);

            var itemToUpdate = itemSqlDatabase.GetById(item.Id);

            itemToUpdate.AddUpdateItem(item, itemSqlDatabase);
            return(itemToUpdate);
        }
Пример #15
0
        public ShoppingItem AddUpdate(ShoppingItem shoppingItem)
        {
            var dbContext = new ShoppingListDbContext();

            var shoppingItemSqlStorage = new ShoppingItemSqlStorage(dbContext);

            var itemToUpdate = shoppingItemSqlStorage.GetById(shoppingItem.Id);

            itemToUpdate.AddUpdateItem(shoppingItem, shoppingItemSqlStorage);
            return(itemToUpdate);
        }
        public Picture AddUpdate(Picture picture)
        {
            var dbContext = new ShoppingListDbContext();

            var pictureSqlStorage = new PictureSqlStorage(dbContext);

            var pictureToUpdate = pictureSqlStorage.GetById(picture.Id);

            pictureToUpdate.AddUpdateItem(picture, pictureSqlStorage);
            return(pictureToUpdate);
        }
Пример #17
0
        public ShoppingEvent AddUpdate(ShoppingEvent shoppingEvent)
        {
            var dbContext = new ShoppingListDbContext();

            var shoppingEventSqlStorage = new ShoppingEventSqlStorage(dbContext);

            var itemToUpdate = shoppingEventSqlStorage.GetById(shoppingEvent.Id);

            itemToUpdate.AddUpdateItem(shoppingEvent, shoppingEventSqlStorage);
            return(itemToUpdate);
        }
Пример #18
0
 public IEnumerable <ItemViewModel> GetItemsByShoppingListID(int?id)
 {
     using (var ctx = new ShoppingListDbContext())
     {
         string query = "SELECT * FROM Item WHERE ShoppingListID = @p0";
         return(ctx.Items.SqlQuery(query, id).Select(
                    e => new ItemViewModel {
             ID = e.ID, ShoppingListID = id.Value, Priority = e.Priority, Content = e.Content, IsChecked = e.IsChecked, CreatedUtc = e.CreatedUtc, ModifiedUtc = e.ModifiedUtc
         }
                    ).ToArray());
     }
 }
Пример #19
0
        public bool DeleteNote(int?id)
        {
            using (var ctx = new ShoppingListDbContext())
            {
                var entity = ctx.Notes.SingleOrDefault(e => e.ItemId == id);

                // TODO: Handle note not found
                ctx.Notes.Remove(entity);

                return(ctx.SaveChanges() == 1);
            }
        }
Пример #20
0
        public bool UpdateList(ShoppingListViewModel vm)
        {
            using (var ctx = new ShoppingListDbContext())
            {
                var entity = ctx.ShoppingLists.SingleOrDefault(e => e.ID == vm.ID);

                entity.Name         = vm.Name;
                entity.Color        = vm.Color;
                entity.Group        = (vm.Group == null || vm.Group == "") ? "General" : vm.Group;
                entity.ModifieddUtc = DateTimeOffset.UtcNow;

                return(ctx.SaveChanges() == 1);
            }
        }
Пример #21
0
        public bool DeleteList(int?id)
        {
            using (var ctx = new ShoppingListDbContext())
            {
                //ctx.Database.ExecuteSqlCommand($"DELETE FROM Item WHERE ShoppingListID = {id}");

                var entity = ctx.ShoppingLists.SingleOrDefault(e => e.ID == id);

                // TODO: Handle note not found
                ctx.ShoppingLists.Remove(entity);

                return(ctx.SaveChanges() == 1);
            }
        }
Пример #22
0
 public void DeleteAllItems(int?shoppingListID, string path)
 {
     using (var ctx = new ShoppingListDbContext())
     {
         //ctx.Database.ExecuteSqlCommand($"DELETE FROM Item WHERE ShoppingListID = {shoppingListID}");
         var items = ctx.Items.Where(item => item.ShoppingListID == shoppingListID);
         foreach (var item in items)
         {
             ctx.Items.Remove(item);
             DeleteFile(path + "\\" + item.ID + ".jpg");
         }
         ctx.SaveChanges();
     }
 }
Пример #23
0
        public int[] UpdateItem(ItemViewModel vm)
        {
            using (var ctx = new ShoppingListDbContext())
            {
                var entity = ctx.Items.SingleOrDefault(e => e.ID == vm.ID);

                entity.Content     = vm.Content;
                entity.Priority    = vm.Priority;
                entity.IsChecked   = vm.IsChecked.Value;
                entity.ModifiedUtc = DateTimeOffset.UtcNow;

                int[] result = new int[] { ctx.SaveChanges(), entity.ID };

                return(result);
            }
        }
Пример #24
0
        public bool CreateList(ShoppingListViewModel vm)
        {
            using (var ctx = new ShoppingListDbContext())
            {
                var entity =
                    new ShoppingList
                {
                    UserID     = _userID,
                    Name       = vm.Name,
                    Color      = vm.Color,
                    Group      = (vm.Group == null || vm.Group == "") ? "General" : vm.Group,
                    CreatedUtc = DateTimeOffset.UtcNow,
                };

                ctx.ShoppingLists.Add(entity);

                return(ctx.SaveChanges() == 1);
            }
        }
Пример #25
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 (var db = new ShoppingListDbContext())
                {
                    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));
        }
Пример #26
0
        public int[] CreateItem(ItemViewModel vm, int shoppingListId)
        {
            using (var ctx = new ShoppingListDbContext())
            {
                var entity =
                    new Item
                {
                    ShoppingListID = shoppingListId,
                    Content        = vm.Content,
                    Priority       = vm.Priority,
                    IsChecked      = vm.IsChecked.Value,
                    CreatedUtc     = DateTimeOffset.UtcNow,
                };

                ctx.Items.Add(entity);

                int[] result = new int[] { ctx.SaveChanges(), entity.ID };

                return(result);
            }
        }
        public SqlShoppingListRepoExampleData(ShoppingListDbContext context,
                                              PredictionEnginePool <GroceryData, GroceryItemPrediction> predictionEnginePool,
                                              IUserPasswordSalt salt) : base(context,
                                                                             predictionEnginePool, salt)
        {
            if (context.ShopWaypointsEntities.ToList().Count == 0)
            {
                var userCreateDto = new UserCreateDto("user", "password");
                var userEntity    = ToUserEntity(userCreateDto);

                context.UserEntities.Add(userEntity);

                SaveChanges();

                context.ShopWaypointsEntities.Add(new ShopWaypointsEntity
                {
                    Id   = 0,
                    Name = "big-market",
                    ShoppingListEntities     = new List <ShoppingListEntity>(),
                    ShopWaypointsReadDtoJson = JsonSerializer.Serialize(WaypointsRepoHardcoded.HardcodedWaypoints)
                });

                SaveChanges();
Пример #28
0
        public static void RegisterAuth()
        {
            var db = new ShoppingListDbContext();
            db.Database.Initialize(false);

            WebSecurity.InitializeDatabaseConnection("DefaultConnection", typeof(UserProfile).Name, "UserId", "UserName", autoCreateTables: true);
            // To let users of this site log in using their accounts from other sites such as Microsoft, Facebook, and Twitter,
            // you must update this site. For more information visit http://go.microsoft.com/fwlink/?LinkID=252166

            //OAuthWebSecurity.RegisterMicrosoftClient(
            //    clientId: "",
            //    clientSecret: "");

            //OAuthWebSecurity.RegisterTwitterClient(
            //    consumerKey: "",
            //    consumerSecret: "");

            OAuthWebSecurity.RegisterFacebookClient(
                appId: Oauth.FbAppId,
                appSecret: Oauth.FbAppSecret);

            OAuthWebSecurity.RegisterGoogleClient();
        }
Пример #29
0
        public static void RegisterAuth()
        {
            var db = new ShoppingListDbContext();

            db.Database.Initialize(false);

            WebSecurity.InitializeDatabaseConnection("DefaultConnection", typeof(UserProfile).Name, "UserId", "UserName", autoCreateTables: true);
            // To let users of this site log in using their accounts from other sites such as Microsoft, Facebook, and Twitter,
            // you must update this site. For more information visit http://go.microsoft.com/fwlink/?LinkID=252166

            //OAuthWebSecurity.RegisterMicrosoftClient(
            //    clientId: "",
            //    clientSecret: "");

            //OAuthWebSecurity.RegisterTwitterClient(
            //    consumerKey: "",
            //    consumerSecret: "");

            OAuthWebSecurity.RegisterFacebookClient(
                appId: Oauth.FbAppId,
                appSecret: Oauth.FbAppSecret);

            OAuthWebSecurity.RegisterGoogleClient();
        }
Пример #30
0
        public NoteViewModel GetNoteByItemID(int?id)
        {
            Note entity;

            using (var ctx = new ShoppingListDbContext())
            {
                entity = ctx.Notes.SingleOrDefault(e => e.ItemId == id);
            }
            if (entity != null)
            {
                return new NoteViewModel
                       {
                           ItemId      = entity.ItemId,
                           Body        = entity.Body,
                           CreatedUtc  = entity.CreatedUtc,
                           ModifiedUtc = entity.ModifiedUtc
                       }
            }
            ;
            else
            {
                return(null);
            }
        }
 public ConsumerRepository(ShoppingListDbContext shoppingListDbContext)
 {
     _shoppingListDbContext = shoppingListDbContext;
 }
Пример #32
0
 public ShoppingListNotificationService(IHubContext <ShoppingListHub> hub, ShoppingListDbContext db)
 {
     this.hub = hub;
     this.db  = db;
 }
        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 (var db = new ShoppingListDbContext())
                {
                    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);
        }
Пример #34
0
 private static string ReadAccessTokenFromDb()
 {
     var db = new ShoppingListDbContext();
     var user = db.UserProfiles.FirstOrDefault(userProfile => userProfile.UserName.ToLower() == Thread.CurrentPrincipal.Identity.Name.ToLower());
     return user != null ? user.AccessToken : null;
 }