Example #1
0
        public async Task <T> Add <T>(T obj)
        {
            using MainContext _db = new MainContext();
            await _db.AddAsync(obj);

            await _db.SaveChangesAsync();

            return(obj);
        }
        async public Task CreateCategory(string title, int userI)
        {
            Category c = new Category();

            c.Title   = title;
            c.AdminId = userI;
            await _context.AddAsync(c);

            await _context.SaveChangesAsync();
        }
Example #3
0
        public async Task <UserLoginResponse> Login(Credentials credentials)
        {
            var user = await _context.User.FirstOrDefaultAsync(x => x.Email == credentials.Email);

            if (user == null || _passwordHasher.VerifyHashedPassword(user, user.Password, credentials.Password) != PasswordVerificationResult.Success)
            {
                return(null);
            }

            var now           = DateTime.Now;
            var expiryDate    = now.AddDays(7);
            var refreshToken  = TokenHelper.GenerateRefreshToken();
            var tokenResponse = new UserToken()
            {
                UserId       = user.Id,
                ExpiryDate   = expiryDate,
                LastUpdated  = now,
                RefreshToken = refreshToken
            };
            await _context.AddAsync(tokenResponse);

            await _context.SaveChangesAsync();

            var defaultShoppingGroup = await _context.ShoppingGroupUser.FirstOrDefaultAsync(x => x.UserId == user.Id && x.Default);

            var tokens = new TokenResponse()
            {
                Id   = user.Id,
                Role = user.Role,
            }.ToToken(_config);

            tokens.RefreshToken = refreshToken;


            return(new UserLoginResponse()
            {
                Token = tokens,
                Email = user.Email,
                Id = user.Id,
                Role = user.Role,
                DefaultShoppingGroupId = defaultShoppingGroup?.ShoppingGroupId,
            });
        }
Example #4
0
        public async Task <Shop> CreateShop(Shop shop)
        {
            if (String.IsNullOrWhiteSpace(shop.Name))
            {
                return(null);
            }
            shop.ShoppingGroupId = 5;
            await _context.AddAsync(shop);

            await _context.SaveChangesAsync();

            return(shop);
        }
Example #5
0
        public async Task <bool> AddAsync(TEntity Entity, bool AutoSave)
        {
            if (AutoSave)
            {
                await db.AddAsync(Entity);

                return(await db.SaveChangesAsync() > 0);
            }
            else
            {
                LstAdd.Add(Entity);
                return(true);
            }
        }
Example #6
0
        public async Task <GroceryItem> AddItemToList(int shopId, GroceryItem grocery)
        {
            if (String.IsNullOrWhiteSpace(grocery.Name))
            {
                return(null);
            }
            var shop = await _context.Shop.FirstOrDefaultAsync(x => x.Id == shopId);

            if (shop == null)
            {
                return(null);
            }
            grocery.Purchased = false;
            grocery.ShopId    = shopId;
            grocery.AddedAt   = DateTime.Now;

            await _context.AddAsync(grocery);

            await _context.SaveChangesAsync();

            return(grocery);
        }
        public async Task <Response <LoginResponse> > Login(Credentials credentials)
        {
            var user = await _context.User.FirstOrDefaultAsync(user => user.Email.ToLower() == credentials.Email.ToLower());

            if (user == null || _passwordHasher.VerifyHashedPassword(user, user.Password, credentials.Password) == PasswordVerificationResult.Failed)
            {
                return(new Response <LoginResponse>()
                {
                    Error = "De combinatie van email en wachtwoord komt niet voor in ons systeem",
                    Success = false
                });
            }

            var myApps = await _context.UserApp.FirstOrDefaultAsync(app => app.AppId == credentials.AppId && app.UserId == user.Id);

            if (myApps == null)
            {
                return new Response <LoginResponse>()
                       {
                           Error   = "U bent niet geautoriseerd om deze app te gebruiken",
                           Success = false
                       }
            }
            ;

            var userToken = new Models.UserToken()
            {
                UserId = user.Id
            };
            var token = userToken.ToToken(_config);

            token.RefreshToken = TokenHelper.GenerateRefreshToken();
            var now = DateTime.Now;

            await _context.AddAsync(new HomeAutomation.Models.Database.Authorization.UserToken()
            {
                UserId       = user.Id,
                RefreshToken = token.RefreshToken,
                LastUpdated  = now,
                ExpiryDate   = now.AddDays(14),
                AppId        = credentials.AppId
            });

            await _context.SaveChangesAsync();

            var defaultAppId = _context.UserApp.FirstOrDefault(x => x.Default).AppId;
            var response     = new LoginResponse()
            {
                TokenSettings  = token,
                Email          = user.Email,
                Id             = user.Id,
                DefaultAppId   = defaultAppId,
                DefaultAppName = _context.App.FirstOrDefault(x => x.Id == defaultAppId).Name
            };

            return(new Response <LoginResponse>()
            {
                Data = response,
                Success = true
            });
        }
Example #8
0
 public async Task Add(Student student)
 {
     _db.AddAsync(student);
     await _db.SaveChangesAsync();
 }
        protected async Task CreateDataAsync <TEntity>(TEntity entity)
        {
            await _mainContext.AddAsync(entity);

            await _mainContext.SaveChangesAsync();
        }