Ejemplo n.º 1
0
        public IActionResult CreateClientActivity(int activityId,
                                                  [FromBody] ClientActivityForCreation clientActivity)
        {
            if (clientActivity == null)
            {
                return(BadRequest());
            }

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

            if (!_sportRepository.ActivityExists(activityId))
            {
                return(NotFound());
            }

            var clientId = User.Claims.FirstOrDefault(c => c.Type == "sub").Value;

            if (_sportRepository.ClientActivityExists(clientId, activityId))
            {
                return(BadRequest("This user has already registered to this activity"));
            }

            var finalClientActivity = Mapper.Map <Entities.ClientActivity>(clientActivity);

            finalClientActivity.ClientId = clientId;

            _sportRepository.AddClientActivity(activityId, finalClientActivity);

            if (!_sportRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            var createdClientActivityToReturn = Mapper.Map <Model.ClientActivity>(finalClientActivity);

            return(CreatedAtRoute("GetClientActivity", new
                                  { /*ActivityId = activityId,*/ id = createdClientActivityToReturn.Id }, createdClientActivityToReturn));
        }