Exemplo n.º 1
0
        public async Task <IActionResult> CreateMatch([FromBody] PostMatch model)
        {
            using MatchHelper mh = new MatchHelper(_dbcontext, _matchRepository, _oddRepository);

            if (ModelState.IsValid)
            {
                //model.MatchDate = DateTime.Today;
                //model.MatchTime = DateTime.Now.TimeOfDay;
                //model.Sport = (int)Sports.Football;
                return(Ok(await mh.InsertMatchWithOdd(model)));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
        //[Authorize]
        public async Task <IActionResult> CreatePost(string Username, PostMatch postMessage)
        {
            var user = await userManager.FindByNameAsync(Username);

            var userRole = await userManager.IsInRoleAsync(user, "admin");

            if (user == null || !userRole)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, new Response {
                    Status = "Error", Message = "Check the username, or visit our register page to have access to this feature"
                }));
            }

            else if (user != null && userRole)
            {
                //var roleName = await userManager.GetUsersInRoleAsync("Admin");

                //if (roleName.Contains(user))
                //{

                Post post = new Post
                {
                    Author    = postMessage.Author,
                    Category  = postMessage.Category,
                    Photo     = postMessage.Photo,
                    PostDate  = postMessage.PostDate,
                    PostTitle = postMessage.PostTitle,
                    PostId    = postMessage.PostId,
                    status    = postMessage.status,
                    BlogPost  = postMessage.BlogPost
                };
                _bloggContext.Posts.Add(post);
                _bloggContext.SaveChanges();
                return(Ok(new Response {
                    Status = "Success", Message = "Post Approved and Created. "
                }));
                //}
            }
            else
            {
                return(Ok(new Response {
                    Status = "Pending Approval", Message = "An Admin has to approve your post first."
                }));
            }
        }
        public async Task <PostMatch> InsertMatchWithOdd(PostMatch model)
        {
            using var transaction = _dbcontext.Database.BeginTransaction(IsolationLevel.ReadCommitted);

            try
            {
                Match match = new Match
                {
                    Description = model.Description,
                    MatchDate   = model.MatchDate,
                    MatchTime   = model.MatchTime,
                    TeamA       = model.TeamA,
                    TeamB       = model.TeamB,
                    Sport       = model.Sport
                };

                _dbcontext.Matches.Add(match);
                await _dbcontext.SaveChangesAsync();

                MatchOdd odd = new MatchOdd
                {
                    MatchId   = match.Id,
                    Specifier = model.Specifier,
                    Odd       = model.Odd
                };

                _dbcontext.MatchOdds.Add(odd);
                await _dbcontext.SaveChangesAsync();

                transaction.Commit();

                return(model);
            }
            catch (Exception e)
            {
                transaction.Rollback();
                return(null);
            }
            finally
            {
                transaction.Dispose();
            }
        }