public async Task <IActionResult> Update([FromBody] InterpreterDetailsModel interpreter)
        {
            if (interpreter == null)
            {
                return(ReturnError(ErrorCodes.IncomingPayloadIsMissing));
            }

            try
            {
                var brokerId = User.TryGetBrokerId().Value;
                if (EnumHelper.GetEnumByCustomName <InterpreterInformationType>(interpreter.InterpreterInformationType) == InterpreterInformationType.NewInterpreter)
                {
                    ReturnError(ErrorCodes.InterpreterFaultyIntention);
                }
                var updatedInterpreter = _apiUserService.GetInterpreter(interpreter, brokerId);
                await _dbContext.SaveChangesAsync();

                var updatedInterpreterResponse = ApiUserService.GetModelFromEntity(updatedInterpreter);
                return(Ok(new UpdateInterpreterResponse {
                    Interpreter = updatedInterpreterResponse
                }));
            }
            catch (InvalidApiCallException ex)
            {
                return(ReturnError(ex.ErrorCode));
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Unexpected error occured when client called Request/{nameof(View)}");
                return(ReturnError(ErrorCodes.UnspecifiedProblem));
            }
        }
Exemple #2
0
        internal InterpreterBroker GetInterpreter(InterpreterDetailsModel interpreterModel, int brokerId, bool updateInformation = true)
        {
            if (interpreterModel == null)
            {
                throw new InvalidApiCallException(ErrorCodes.InterpreterAnswerNotValid);
            }
            InterpreterBroker interpreter = null;

            switch (EnumHelper.GetEnumByCustomName <InterpreterInformationType>(interpreterModel.InterpreterInformationType))
            {
            case InterpreterInformationType.ExistingInterpreter:
                interpreter = _dbContext.InterpreterBrokers
                              .SingleOrDefault(i => i.InterpreterBrokerId == interpreterModel.InterpreterId && i.BrokerId == brokerId);
                break;

            case InterpreterInformationType.AuthorizedInterpreterId:
                interpreter = _dbContext.InterpreterBrokers
                              .SingleOrDefault(i => i.OfficialInterpreterId == interpreterModel.OfficialInterpreterId && i.BrokerId == brokerId);
                break;

            case InterpreterInformationType.NewInterpreter:
                //check if unique officialInterpreterId for broker
                if (_interpreterService.IsUniqueOfficialInterpreterId(interpreterModel.OfficialInterpreterId, brokerId))
                {
                    //Create the new interpreter, connected to the provided broker
                    var newInterpreter = new InterpreterBroker(
                        interpreterModel.FirstName,
                        interpreterModel.LastName,
                        brokerId,
                        interpreterModel.Email,
                        interpreterModel.PhoneNumber,
                        interpreterModel.OfficialInterpreterId
                        );
                    _dbContext.Add(newInterpreter);
                    return(newInterpreter);
                }
                else
                {
                    throw new InvalidApiCallException(ErrorCodes.InterpreterOfficialIdAlreadySaved);
                }

            default:
                return(null);
            }
            if (updateInformation)
            {
                interpreter.IsActive = interpreterModel.IsActive;
                if (!string.IsNullOrWhiteSpace(interpreterModel.FirstName))
                {
                    interpreter.FirstName = interpreterModel.FirstName;
                }
                if (!string.IsNullOrWhiteSpace(interpreterModel.LastName))
                {
                    interpreter.LastName = interpreterModel.LastName;
                }
                if (!string.IsNullOrWhiteSpace(interpreterModel.Email))
                {
                    interpreter.Email = interpreterModel.Email;
                }
                if (!string.IsNullOrWhiteSpace(interpreterModel.PhoneNumber))
                {
                    interpreter.PhoneNumber = interpreterModel.PhoneNumber;
                }
                if (!string.IsNullOrWhiteSpace(interpreterModel.OfficialInterpreterId))
                {
                    if (_interpreterService.IsUniqueOfficialInterpreterId(interpreterModel.OfficialInterpreterId, brokerId, interpreter.InterpreterBrokerId))
                    {
                        interpreter.OfficialInterpreterId = interpreterModel.OfficialInterpreterId;
                    }
                    else
                    {
                        throw new InvalidApiCallException(ErrorCodes.InterpreterOfficialIdAlreadySaved);
                    }
                }
            }
            return(interpreter);
        }