/// <summary>
        /// Crea un supervisor en el sistema.
        /// </summary>
        /// <param name="supervisorData">Datos del supervisor a crear.</param>
        /// <returns>
        /// Modelo <see cref="PostBOSupervisorResponseModel"/> con los datos de la respuesta.
        /// </returns>
        public PostBOSupervisorResponseModel CreateSupervisor(PostBOSupervisorRequestModel supervisorData)
        {
            PostBOSupervisorResponseModel result = new PostBOSupervisorResponseModel
            {
                Status = ResultStatus.ERROR
            };

            var context = _supervisorRepository.GetContext();

            using (var dbTransaction = context.Database.BeginTransaction())
            {
                try
                {
                    var toInsert = new Supervisors()
                    {
                        Name     = supervisorData.Name,
                        Email    = supervisorData.Email,
                        Password = CreateHash(supervisorData.Password),
                        State    = 0
                    };

                    // Se da de alta en BD
                    _supervisorRepository.Insert(toInsert);
                    _supervisorRepository.Save();

                    dbTransaction.Commit();
                    result.Status = ResultStatus.SUCCESS;
                    return(result);
                }
                catch (DbUpdateException updEx)
                {
                    dbTransaction.Rollback();

                    SqlException sqlEx = updEx.GetBaseException() as SqlException;
                    if (sqlEx != null)
                    {
                        switch (sqlEx.Number)
                        {
                        case 515: // NOT NULL values
                            // Determino nombre de la columna que no acepta valor NULL
                            // EJ: "Cannot insert the value NULL into column 'SiebelId', table 'InConcert.dbo.Commercials'."
                            int    indexStart = sqlEx.Message.IndexOf("'", 0) + 1;
                            int    indexEnd   = sqlEx.Message.IndexOf("'", indexStart);
                            string columName  = sqlEx.Message[indexStart..indexEnd];
Пример #2
0
        public IActionResult CreateSupervisor([FromBody][Required] PostBOSupervisorRequestDto data)
        {
            try
            {
                /// <summary>
                /// Método para guardar request en logs
                /// </summary>
                _securityService.RequestLog("api/bo/supervisors/CreateSupervisor: " + JsonConvert.SerializeObject(data));

                PostBOSupervisorRequestModel  request          = _mapper.Map <PostBOSupervisorRequestModel>(data);
                PostBOSupervisorResponseModel businessResponse = _boSupervisorService.CreateSupervisor(request);

                switch (businessResponse.Status)
                {
                case ResultStatus.SUCCESS:
                    return(Ok());

                case ResultStatus.BAD_REQUEST:
                    return(BadRequest());

                case ResultStatus.ACCESS_DENIED:
                    return(StatusCode(StatusCodes.Status401Unauthorized));

                case ResultStatus.NOT_AUTHORIZED:
                    return(StatusCode(StatusCodes.Status403Forbidden));

                case ResultStatus.NOT_NULL:
                case ResultStatus.SUPERVISOR_ROW_DUPLICATE:
                case ResultStatus.SUPERVISOR_ROW_DUPLICATE_EMAIL:
                    StatusResponseDto responseDto = _mapper.Map <StatusResponseDto>(businessResponse);
                    return(StatusCode(StatusCodes.Status409Conflict, responseDto));

                default:
                    return(StatusCode(StatusCodes.Status500InternalServerError, businessResponse.Message));
                }
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e));
            }
        }