Exemplo n.º 1
0
        public async Task <IActionResult> Update([FromBody] CurrencyDto currencyDto)
        {
            Currency currency = await currencyRepository.GetCurrency(currencyDto.Id);

            if (currency == null)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, $"Currency id == {currencyDto.Id}. Not found!"));
            }
            if (currency.Code != currencyDto.Code)
            {
                if (await currencyRepository.GetCurrencyByCode(currencyDto.Code) != null)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, $"Currency with this Name == {currencyDto.Code} already exists"));
                }

                currency.SetCode(currencyDto.Code);
            }

            if (currency.Name != currencyDto.Name)
            {
                if (await currencyRepository.GetCurrency(currencyDto.Name) != null)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, $"Currency with this PublicId == {currencyDto.Name} already exists"));
                }
                currency.SetName(currencyDto.Name);
            }

            await currencyRepository.Update(currency);

            await unitOfWork.SaveEntitiesAsync();

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Add([FromBody] CurrencyDto currencyDto)
        {
            if (await currencyRepository.GetCurrencyByCode(currencyDto.Code) != null)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, $"Currency with this PublicId == {currencyDto.Code} already exists"));
            }
            if (await currencyRepository.GetCurrency(currencyDto.Name) != null)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, $"Currency with this Name == {currencyDto.Name} already exists"));
            }
            Currency currency = new Currency(currencyDto.Code, currencyDto.Name);
            await currencyRepository.Add(currency);

            await unitOfWork.SaveEntitiesAsync();

            return(Ok());
        }