Ejemplo n.º 1
0
        public async Task <IActionResult> AddBonus(AddBonusDto bonusDto)
        {
            // get current user. This can be further extracted into a helper function
            // or added to a custom, base controller that the other controllers inherit from
            var userClaims = this.User.Claims;
            var userId     = userClaims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                var service = await _serviceService.AddBonus(bonusDto, userId);

                if (service == null)
                {
                    return(BadRequest("User or service does not exist."));
                }

                return(Ok(service));
            }
            catch (Exception ex)
            {
                if (ex.Message.ToLower() == "conflict")
                {
                    return(Conflict($"You already have an active bonus for this service."));
                }

                return(StatusCode(500));
            }
        }