예제 #1
0
        public IActionResult addDog([FromBody] Dog newDog)
        {
            newDog.Owner = null;
            var claimsIdentity = this.User.Identity as ClaimsIdentity;
            var userName       = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;

            if (userName == null)
            {
                return(BadRequest(new { message = "Błąd autoryzacji" }));
            }
            int userId = int.Parse(userName);

            if (userId != newDog.OwnerId)
            {
                return(Unauthorized());
            }

            try
            {
                if (!appSettingsService.canEnter())
                {
                    throw new AppException("Akcja obecnie niedozwolona");
                }
                Dog addedDog = dogService.addDog(newDog);
                if (addedDog == null)
                {
                    return(BadRequest(new { message = "Błąd dodawania psa!" }));
                }
                return(Ok());
            }
            catch (AppException ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }
예제 #2
0
        public IActionResult addContest([FromBody] ContestTypeDTO newContestType)
        {
            var claimsIdentity = this.User.Identity as ClaimsIdentity;

            try
            {
                userService.IsUserAnOrganizator(claimsIdentity);
                if (!appSettingsService.canEnter())
                {
                    throw new AppException("Akcja obecnie niedozwolona");
                }
                List <AllowedBreedsContest> allowedBreeds = new List <AllowedBreedsContest>();
                foreach (int breedId in newContestType.breedIds)
                {
                    allowedBreeds.Add(new AllowedBreedsContest
                    {
                        BreedTypeId = breedId
                    });
                }
                ContestType contestType = new ContestType
                {
                    Enterable            = newContestType.isEnterable,
                    NamePolish           = newContestType.name,
                    AllowedBreedsContest = allowedBreeds
                };
                ContestType savedContestType = contestService.addContest(contestType);
                if (savedContestType == null)
                {
                    throw new AppException("Błąd tworzenia konkursu");
                }
                return(Ok());
            }
            catch (Exception e)
            {
                return(BadRequest(new { message = e.Message }));
            }
        }