Beispiel #1
0
        public IHttpActionResult DeleteStrategy(Guid id)
        {
            CSGOStrategy entity = UserCanEdit(id);

            if (entity == null)
            {
                return(BadRequest("You need to be team editor."));
            }

            if (entity.TeamId != null && entity.TeamId != Guid.Empty)
            {
                if (!UserIsTeamEditor(entity.TeamId.Value))
                {
                    return(BadRequest("You need to be team editor to delete this strategy."));
                }
            }

            _dbContext.Strategies.Remove(entity);

            try
            {
                _dbContext.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                System.Diagnostics.Trace.TraceError("Delete user strategy error: " + e.Message);
                return(BadRequest("Something went wrong..."));
            }
            return(Ok("Ok"));
        }
        public async Task <IActionResult> SubmitStrategy(CSGOStrategy strategy)
        {
            if (strategy.TeamId != null && strategy.TeamId != Guid.Empty)
            {
                if (!await UserIsTeamEditor(strategy.TeamId.Value))
                {
                    return(BadRequest("You need to be team editor."));
                }
            }

            CSGOStrategy entity = null;

            if (strategy.Id != Guid.Empty)
            {
                entity = await UserCanEdit(strategy.Id);
            }

            if (entity == null)
            {
                ApplicationUser user = await GetAuthUser();

                strategy.UserId = user.Id;
                strategy.UniqueCustomUrl(_dbContext);
                _dbContext.Attach(strategy).State = EntityState.Added;
            }
            else
            {
                if (string.IsNullOrEmpty(entity.UserId))
                {
                    ApplicationUser user = await GetAuthUser();

                    strategy.UserId = user.Id;
                }
                strategy.LastUpdated = DateTimeOffset.Now;
                if (!Uri.IsWellFormedUriString(strategy.StratImage, UriKind.Absolute))
                {
                    try
                    {
                        strategy.StratImage = await _fileService.SaveImage(strategy.StratImage, strategy.Id.ToString());
                    }
                    catch (Exception e)
                    {
                        return(BadRequest(e.Message));
                    }
                }
                _dbContext.Entry(entity).CurrentValues.SetValues(strategy);
            }

            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                System.Diagnostics.Trace.TraceError("User Strategy submit error: " + e.Message);
                return(BadRequest("Something went wrong..."));
            }
            return(Ok(strategy));
        }
Beispiel #3
0
        private CSGOStrategy ResolveStrategy(string stratId)
        {
            CSGOStrategy strat = _dbContext.Strategies.FirstOrDefault(s => s.CustomUrl == stratId);

            if (strat == null)
            {
                var valid = Guid.TryParse(stratId, out Guid id);
                if (valid)
                {
                    strat = _dbContext.Strategies.Find(id);
                }
            }
            return(strat);
        }
Beispiel #4
0
        public IHttpActionResult GetStrat(string stratId)
        {
            CSGOStrategy strat = ResolveStrategy(stratId);

            if (strat != null && !strat.Visible && strat.TeamId != null && strat.TeamId != Guid.Empty)
            {
                if (!UserIsTeamMember(strat.TeamId.Value))
                {
                    return(BadRequest("You need to be team editor."));
                }
            }

            if (strat != null)
            {
                return(Ok(strat));
            }
            return(BadRequest("Strat not found or user is not team member."));
        }
Beispiel #5
0
        private CSGOStrategy UserCanEdit(Guid id)
        {
            ApplicationUser user  = GetAuthUser();
            CSGOStrategy    strat = _dbContext.Strategies.Find(id);

            if (strat?.TeamId != null)
            {
                if (strat.Team.Members.Any(m => m.IsEditor || m.IsAdmin && m.UserId == user.Id))
                {
                    return(strat);
                }
            }
            else if (strat?.UserId == user.Id)
            {
                return(strat);
            }
            return(null);
        }
Beispiel #6
0
        public IHttpActionResult SubmitStrategy(CSGOStrategy strategy)
        {
            if (strategy.TeamId != null && strategy.TeamId != Guid.Empty)
            {
                if (!UserIsTeamEditor(strategy.TeamId.Value))
                {
                    return(BadRequest("You need to be team editor."));
                }
            }

            CSGOStrategy entity = UserCanEdit(strategy.Id);

            if (entity == null)
            {
                ApplicationUser user = GetAuthUser();
                strategy.UserId = user.Id;
                strategy.UniqueCustomUrl(_dbContext);
                strategy.SaveStrategyImage();
                entity = _dbContext.Strategies.Add(strategy);
            }
            else
            {
                if (string.IsNullOrEmpty(entity.UserId))
                {
                    ApplicationUser user = GetAuthUser();
                    strategy.UserId = user.Id;
                }
                strategy.LastUpdated = DateTimeOffset.Now;
                strategy.SaveStrategyImage();
                _dbContext.Entry(entity).CurrentValues.SetValues(strategy);
            }

            try
            {
                _dbContext.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                System.Diagnostics.Trace.TraceError("User Strategy submit error: " + e.Message);
                return(BadRequest("Something went wrong..."));
            }
            return(Ok(entity));
        }
        private async Task <CSGOStrategy> UserCanEdit(Guid id)
        {
            ApplicationUser user = await GetAuthUser();

            CSGOStrategy strat = await _dbContext.CSGOStrategies.Include(s => s.Team).ThenInclude(t => t.Members).FirstOrDefaultAsync(s => s.Id == id);

            if (strat?.TeamId != null)
            {
                if (strat.Team.Members.Any(m => m.UserId == user.Id && m.IsEditor || m.IsAdmin))
                {
                    return(strat);
                }
            }
            else if (strat?.UserId == user.Id)
            {
                return(strat);
            }
            return(null);
        }
        public async Task <IActionResult> SubmitStrategyComment(StrategyComment comment)
        {
            ApplicationUser user = await GetAuthUser();

            CSGOStrategy strat = null;

            comment.User = user;

            var entity = await _dbContext.StrategyComments.FindAsync(comment.Id);

            if (entity != null)
            {
                _dbContext.Entry(entity).CurrentValues.SetValues(comment);
            }
            else
            {
                _dbContext.StrategyComments.Add(comment);
                strat = await _dbContext.CSGOStrategies.FindAsync(comment.StratId);
            }

            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                System.Diagnostics.Trace.TraceError("Comment user strategy error: " + e.Message);
                return(BadRequest("Something went wrong..."));
            }

            if (strat != null && strat.UserId != user.Id)
            {
                List <BellumGensPushSubscription> subs = await _dbContext.BellumGensPushSubscriptions.Where(s => s.UserId == comment.Strategy.UserId).ToListAsync();

                await _notificationService.SendNotificationAsync(subs, comment);
            }
            return(Ok(comment));
        }
Beispiel #9
0
        public IHttpActionResult SubmitStrategyComment(StrategyComment comment)
        {
            string       userId = GetAuthUser().Id;
            CSGOStrategy strat  = null;

            comment.User = _dbContext.Users.Find(userId);

            var entity = _dbContext.StrategyComments.Find(comment.Id);

            if (entity != null)
            {
                _dbContext.Entry(entity).CurrentValues.SetValues(comment);
            }
            else
            {
                comment = _dbContext.StrategyComments.Add(comment);
                strat   = _dbContext.Strategies.Find(comment.StratId);
            }

            try
            {
                _dbContext.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                System.Diagnostics.Trace.TraceError("Comment user strategy error: " + e.Message);
                return(BadRequest("Something went wrong..."));
            }

            if (strat != null && strat.UserId != userId)
            {
                List <BellumGensPushSubscription> subs = _dbContext.PushSubscriptions.Where(s => s.userId == comment.Strategy.UserId).ToList();
                NotificationsService.SendNotification(subs, comment);
            }
            return(Ok(comment));
        }