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));
        }
Esempio n. 2
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));
        }