Exemplo n.º 1
0
        public async Task <IActionResult> PutQuestions(int id, Questions questions)
        {
            if (id != questions.questionID)
            {
                return(BadRequest());
            }

            _context.Entry(questions).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!QuestionsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> AddAdventure([FromBody] Adventures adventure)
        {
            _context.Adventures.Add(adventure);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("AddAdventure", new { id = adventure.adventureId }, adventure));
        }
        public async Task <IActionResult> CreatePoll(Poll poll)
        {
            _context.Poll.Add(poll);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetPoll), new { id = poll.Poll_ID }, poll));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> AddGenre([FromBody] Genre genre)
        {
            try
            {
                genre.CreatedDate = DateTime.UtcNow;
                _context.Genre.Add(genre);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("addGenre", new { id = genre.GenreID }, genre));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(null);
            }
        }
Exemplo n.º 5
0
        public async Task <IHttpActionResult> FacebookUpdate(AccountFacebookConnect model)
        {
            var client = new FacebookClient(model.AccessToken);

            client.AppId     = AppConfig.Settings.FacebookId;
            client.AppSecret = AppConfig.Settings.FacebookSecret;

            dynamic fbresult = client.Get("me?fields=id,email,first_name,last_name,gender,locale,link,timezone,location,picture");

            var social = await AppDatabase.UserFacebookClaims.FindAsync(fbresult.id);

            var user = await AppDatabase.Users.FindAsync(UserId);

            if (social == null)
            {
                social             = new UserFacebookClaim();
                social.Id          = fbresult.id;
                social.User        = user;
                social.UserId      = UserId;
                social.AccessToken = model.AccessToken;
                social.Provider    = APIConstants.FACEBOOK;
                AppDatabase.UserFacebookClaims.Add(social);
            }

            FacebookUpdateInternal(social, fbresult);

            await AppDatabase.SaveChangesAsync();

            Session.UpdateFrom(user);
            return(Ok(GetAccountDetails()));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> AddFavourites([FromBody] Favourites favourites)
        {
            try
            {
                favourites.CreatedDate = DateTime.UtcNow;
                _context.Favourites.Add(favourites);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("addFavourites", new { id = favourites.FavouritesId }, favourites));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(null);
            }
        }
Exemplo n.º 7
0
        public async Task <IHttpActionResult> Reset(AccountEmailReset model)
        {
            var user = await AppDatabase.Users.FirstOrDefaultAsync(o => o.Email == model.Email);

            if (user == null)
            {
                return(BadRequest("User not found"));
            }

            if (string.IsNullOrEmpty(user.Email))
            {
                return(BadRequest("User lacks a valid email address. Did you use Facebook or Twilio ?"));
            }

            var token = Strings.RandomString(6).ToLower();

            user.ModifiedOn    = DateTime.UtcNow;
            user.EmailPassword = UserPassword.Create(token);

            AppDatabase.Entry(user).State = EntityState.Modified;

            await AppDatabase.SaveChangesAsync();

            await SendTokenEmail(new UserTokenViewModel
            {
                UserId    = user.Id,
                UserEmail = user.Email,
                Token     = token,
            });

            return(Ok(GetAccountDetails()));
        }
Exemplo n.º 8
0
        public async Task <IHttpActionResult> Update(GameScore score)
        {
            //Cleanup. This should be a background job.
            var badRange = AppDatabase.Scores.Where(o => o.CreatedOn < DateTime.UtcNow.Subtract(new TimeSpan(30, 0, 0, 0)));

            AppDatabase.Scores.RemoveRange(badRange);
            await AppDatabase.SaveChangesAsync();

            // Select old
            var model = await AppDatabase.Scores.OrderByDescending(o => o.Score).FirstOrDefaultAsync(o => o.UserId == UserId);


            //Update ?
            if (model != null)
            {
                //Skip if less than
                if (model.Score > score.Score)
                {
                    return(Ok());
                }

                model.Score = score.Score;
            }
            else
            {
                //Create
                AppDatabase.Scores.Add(score);
            }

            await AppDatabase.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 9
0
        public async Task <ActionResult <PollOption> > AddVote(int id, PollOption pollOption)
        {
            if (id != pollOption.Option_ID)
            {
                return(BadRequest());
            }

            var updatePollOption = await _context.PollOption.FirstOrDefaultAsync(s => s.Option_ID == pollOption.Option_ID);

            _context.Entry(updatePollOption).State = EntityState.Modified;

            updatePollOption.Vote = pollOption.Vote;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PollOptionExist(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(updatePollOption);
        }
        public async Task <IHttpActionResult> Delete(string id)
        {
            var entity = await AppDatabase.Storage.FindAsync(id);

            if (entity == null)
            {
                return(Ok());
            }

            // ACL
            switch (entity.AclType)
            {
            case StorageACLType.Admin:
                if (!User.Identity.IsAuthenticated)
                {
                    return(Unauthorized());
                }
                break;

            case StorageACLType.User:
                if (UserId != entity.AclParam)
                {
                    return(Unauthorized());
                }
                break;
            }

            AppDatabase.Storage.Remove(entity);

            await AppDatabase.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 11
0
        public async Task <IHttpActionResult> FacebookDisconnect(AccountFacebookDisconnect model)
        {
            var user = await AppDatabase.Users.Include(o => o.UserFacebookClaims).FirstOrDefaultAsync(o => o.Id == UserId);

            if (user == null)
            {
                return(BadRequest("User not found"));
            }


            var social = user.UserFacebookClaims.FirstOrDefault();

            if (social == null)
            {
                return(BadRequest("Social connection not found"));
            }

            if (user.UserFacebookClaims.Count() == 1 && string.IsNullOrEmpty(user.Email))
            {
                return(BadRequest("Orphan Account. Please add an email."));
            }

            AppDatabase.UserFacebookClaims.Remove(social);
            await AppDatabase.SaveChangesAsync();

            return(Ok(GetAccountDetails()));
        }
Exemplo n.º 12
0
        public async Task <ActionResult> AddMovie([FromBody] Movie movie)
        {
            try
            {
                movie.CreatedDate = DateTime.UtcNow;
                movie.UpdatedDate = DateTime.UtcNow;
                _context.Movie.Add(movie);
                await _context.SaveChangesAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(null);
            }
        }
        public async Task <IHttpActionResult> UpdateSet(StorageObject[] cloudObjects)
        {
            if (!cloudObjects.Any())
            {
                return(Ok());
            }

            foreach (var cloudObject in cloudObjects)
            {
                var entity = await AppDatabase.Storage.FindAsync(cloudObject.ObjectId);

                if (entity == null)
                {
                    cloudObject.ModifiedOn = cloudObject.CreatedOn = DateTime.UtcNow;

                    AppDatabase.Storage.Add(cloudObject);
                }
                else
                {
                    // ACL
                    switch (entity.AclType)
                    {
                    case StorageACLType.Admin:
                        if (!User.Identity.IsAuthenticated)
                        {
                            return(Unauthorized());
                        }
                        break;

                    case StorageACLType.User:
                        if (UserId != entity.AclParam)
                        {
                            return(Unauthorized());
                        }
                        break;
                    }

                    if (entity.ObjectType != cloudObject.ObjectType)
                    {
                        //would cause caching errors
                        return(BadRequest("Can not change object type"));
                    }

                    entity.ModifiedOn  = DateTime.UtcNow;
                    entity.ObjectData  = cloudObject.ObjectData;
                    entity.ObjectScore = cloudObject.ObjectScore;
                }
            }

            await AppDatabase.SaveChangesAsync();

            return(Ok());
        }
        public async Task <ActionResult <User> > CreateName(User user)
        {
            object userAt;

            var users = await _context.User.Where(u => u.Name == user.Name).FirstOrDefaultAsync();

            if (users != null && users.User_ID > 0)
            {
                userAt = new { id = users.User_ID };
            }
            else
            {
                user.Poll = null;
                user.Vote = null;

                _context.User.Add(user);
                await _context.SaveChangesAsync();

                userAt = new { id = user.User_ID };
            }

            return(CreatedAtAction(nameof(GetUser), userAt, User));
        }
Exemplo n.º 15
0
        async Task DeleteUser(UserAccount user)
        {
            // delete
            AppDatabase.Entry(user).State = EntityState.Deleted;
            AppDatabase.Users.Remove(user);
            AppDatabase.UserFacebookClaims.RemoveRange(AppDatabase.UserFacebookClaims.Where(o => o.UserId == user.Id));

            AppDatabase.Storage.RemoveRange(AppDatabase.Storage.Where(o => o.AclParam == user.Id));
            AppDatabase.Scores.RemoveRange(AppDatabase.Scores.Where(o => o.UserId == user.Id));

            await AppDatabase.SaveChangesAsync();

            Session.SignOut();
            Authorization.SignOut();
        }
Exemplo n.º 16
0
        public async Task <IActionResult> UpdateCell([FromBody] UpdateCellModel data)
        {
            var tableRow = _context.Canvas
                           .Include(c => c.ColorData)
                           .OrderByDescending(c => c.CanvasID)
                           .FirstOrDefault()
                           .ColorData
                           .First(row => row.RowIndex == data.Row && row.ColumnIndex == data.Column);

            tableRow.Hex = data.Hex;

            await _context.SaveChangesAsync();

            return(NoContent());
        }
        public async Task <IHttpActionResult> UpdateDelta(StorageDelta model)
        {
            var entity = await AppDatabase.Storage.FindAsync(model.ObjectId);

            if (entity == null)
            {
                return(Ok());
            }

            // ACL
            switch (entity.AclType)
            {
            case StorageACLType.Admin:
                if (!User.Identity.IsAuthenticated)
                {
                    return(Unauthorized());
                }
                break;

            case StorageACLType.User:
                if (UserId != entity.AclParam)
                {
                    return(Unauthorized());
                }
                break;
            }

            var data = entity.GetData();

            var token = data.GetValue(model.PropertyName);

            if (model.IsFloat)
            {
                data[model.PropertyName] = token.Value <float>() + model.Delta;
            }
            else
            {
                data[model.PropertyName] = token.Value <int>() + (int)model.Delta;
            }

            entity.ObjectData = data.ToString();
            entity.ModifiedOn = DateTime.UtcNow;

            //  Context.Update(entity);
            await AppDatabase.SaveChangesAsync();

            return(Ok());
        }
        public async Task <IHttpActionResult> Create(StorageObject cloudObject)
        {
            cloudObject.ModifiedOn = cloudObject.CreatedOn = DateTime.UtcNow;

            var entity = await AppDatabase.Storage.FindAsync(cloudObject.ObjectId);

            if (entity != null)
            {
                return(BadRequest("ObjectId is in use"));
            }

            AppDatabase.Storage.Add(cloudObject);
            await AppDatabase.SaveChangesAsync();

            return(Ok(cloudObject.GetData()));
        }
Exemplo n.º 19
0
        async Task <IHttpActionResult> Create(AccountEmailSignIn model)
        {
            if (string.IsNullOrEmpty(model.UserId))
            {
                model.UserId = Guid.NewGuid().ToString();
            }

            if (await AppDatabase.Users.AnyAsync(o => o.Id == model.UserId))
            {
                return(BadRequest("UserId is in use."));
            }
            if (await AppDatabase.Users.AnyAsync(o => o.Email == model.Email))
            {
                return(BadRequest("Email is in use."));
            }

            var user = new UserAccount
            {
                Email         = model.Email,
                Id            = model.UserId,
                CreatedOn     = DateTime.UtcNow,
                ModifiedOn    = DateTime.UtcNow,
                EmailPassword = UserPassword.Create(model.Password),
                PhonePassword = UserPassword.Create(model.Password),
            };

            AppDatabase.Users.Add(user);

            await AppDatabase.SaveChangesAsync();

            await SendWelcomeMail(new UserEmailViewModel
            {
                UserId    = user.Id,
                UserEmail = user.Email
            });


            //Sign In
            Authorization.UpdateFrom(user);
            Session.UpdateFrom(user);

            return(Ok(GetAccountDetails()));
        }
        public async Task <IHttpActionResult> Update(StorageObject cloudObject)
        {
            var entity = await AppDatabase.Storage.FindAsync(cloudObject.ObjectId);

            if (entity == null)
            {
                return(await Create(cloudObject));
            }

            // ACL
            switch (entity.AclType)
            {
            case StorageACLType.Admin:
                if (!User.Identity.IsAuthenticated)
                {
                    return(Unauthorized());
                }
                break;

            case StorageACLType.User:
                if (UserId != entity.AclParam)
                {
                    return(Unauthorized());
                }
                break;
            }

            if (entity.ObjectType != cloudObject.ObjectType)
            {
                //would cause caching errors
                return(BadRequest("Can not change object type"));
            }

            entity.ModifiedOn  = DateTime.UtcNow;
            entity.ObjectData  = cloudObject.ObjectData;
            entity.ObjectScore = cloudObject.ObjectScore;

            //  Context.Update(entity);
            await AppDatabase.SaveChangesAsync();

            return(Ok(entity.GetData()));
        }
        public async Task <IHttpActionResult> UpdateProperty(StorageProperty model)
        {
            var entity = await AppDatabase.Storage.FindAsync(model.ObjectId);

            if (entity == null)
            {
                return(Ok());
            }

            // ACL
            switch (entity.AclType)
            {
            case StorageACLType.Admin:
                if (!User.Identity.IsAuthenticated)
                {
                    return(Unauthorized());
                }
                break;

            case StorageACLType.User:
                if (UserId != entity.AclParam)
                {
                    return(Unauthorized());
                }
                break;
            }

            var data = entity.GetData();

            data[model.PropertyName] = model.PropertyValue;

            entity.ObjectData = data.ToString();
            entity.ModifiedOn = DateTime.UtcNow;

            //  Context.Update(entity);
            await AppDatabase.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 22
0
        public async Task <IHttpActionResult> Update(AccountEmailUpdate model)
        {
            var user = await AppDatabase.Users.FindAsync(Session.UserId);

            // no user? signUp. This happens from guest upgrades.
            if (user == null)
            {
                return(await Create(new AccountEmailSignIn
                {
                    UserId = Session.UserId,
                    Email = model.NewEmail,
                    Password = model.NewPassword
                }));
            }

            // make sure Email is not in use by someone else
            string oldEmail = null;

            if (!string.IsNullOrEmpty(model.NewEmail))
            {
                var users = await AppDatabase.Users
                            .Where(o => o.Id != Session.UserId)
                            .AnyAsync(o => o.Email == model.NewEmail);

                if (users)
                {
                    return(BadRequest("Email is in use."));
                }

                oldEmail   = user.Email;
                user.Email = model.NewEmail;
            }

            // update password
            if (!string.IsNullOrEmpty(model.NewPassword))
            {
                user.EmailPassword = UserPassword.Create(model.NewPassword);
            }

            // update password
            if (!string.IsNullOrEmpty(model.NewEmail))
            {
                user.Email = model.NewEmail;
            }

            // update
            user.ModifiedOn = DateTime.UtcNow;
            AppDatabase.Entry(user).State = EntityState.Modified;
            await AppDatabase.SaveChangesAsync();

            await SendUpdateEmail(new UserUpdateViewModel
            {
                UserId    = user.Id,
                UserEmail = user.Email,
                OldEmail  = oldEmail,
            });

            //update session
            Session.UpdateFrom(user);
            Authorization.UpdateFrom(user);

            return(Ok(GetAccountDetails()));
        }
Exemplo n.º 23
0
        public async Task <IHttpActionResult> FacebookCreate(AccountFacebookConnect model)
        {
            var client = new FacebookClient(model.AccessToken);

            client.AppId     = AppConfig.Settings.FacebookId;
            client.AppSecret = AppConfig.Settings.FacebookSecret;

            dynamic fbresult = client.Get("me?fields=id,email,first_name,last_name,gender,locale,link,timezone,location,picture");
            string  email    = fbresult.email;

            var social = await AppDatabase.UserFacebookClaims.FindAsync(fbresult.id);

            if (social != null)
            {
                // old profile
                FacebookUpdateInternal(social, fbresult);
                await AppDatabase.SaveChangesAsync();

                var oldUser = social.User;
                Session.UpdateFrom(oldUser);
                return(Ok(GetAccountDetails()));
            }

            //email in use ?
            var user3 = await AppDatabase.Users.FirstOrDefaultAsync(o => o.Email == email);

            if (user3 != null)
            {
                return(BadRequest("Email is in use. Try account recovery."));
            }

            // new user
            var password = new string(Guid.NewGuid().ToString().Take(7).ToArray());
            var user     = new UserAccount
            {
                CreatedOn     = DateTime.UtcNow,
                ModifiedOn    = DateTime.UtcNow,
                Email         = email,
                Id            = Guid.NewGuid().ToString(),
                EmailPassword = UserPassword.Create(password),
            };

            AppDatabase.Users.Add(user);

            social = new UserFacebookClaim
            {
                Id          = fbresult.id,
                UserId      = user.Id,
                User        = user,
                AccessToken = model.AccessToken
            };

            FacebookUpdateInternal(social, fbresult);

            AppDatabase.UserFacebookClaims.Add(social);

            await SendWelcomeMail(new UserEmailViewModel
            {
                UserId    = user.Id,
                UserEmail = user.Email
            });

            await AppDatabase.SaveChangesAsync();

            Session.UpdateFrom(user);
            return(Ok(GetAccountDetails()));
        }