コード例 #1
0
        public async Task <IHttpActionResult> UpdateCurrency(string isoCode,
                                                             [FromBody] CurrencyBindingModel currency)
        {
            if (currency == null)
            {
                return(BadRequest($"Request content is empty"));
            }
            using (IUnitOfWork rep = Store.CreateUnitOfWork())
            {
                var item = await rep.CurrencyRepository.GetAsync(isoCode);

                if (item == null)
                {
                    return(NotFound());
                }

                item.IsoCode = currency.IsoCode.ToUpper();
                item.Name    = currency.Name;

                await rep.CompleteAsync();

                var result   = item.AsCurrencyDTO(Url);
                var response = Request.CreateResponse(HttpStatusCode.Created, result);
                response.Headers.Location = new Uri(result.GetUrl);
                return(ResponseMessage(response));
            }
        }
コード例 #2
0
        public async Task <IHttpActionResult> PostCurrency([FromBody] CurrencyBindingModel currency)
        {
            if (currency == null)
            {
                return(BadRequest($"Request content is empty"));
            }
            using (IUnitOfWork rep = Store.CreateUnitOfWork())
            {
                var item = await rep.CurrencyRepository.GetAsync(currency.IsoCode);

                if (item != null)
                {
                    return(BadRequest("Currency already exists"));
                }

                var newCurrency = new Currency()
                {
                    IsoCode   = currency.IsoCode.ToUpper(),
                    Name      = currency.Name,
                    Countries = new List <Country>(),
                };

                if (currency.Countries != null)
                {
                    foreach (var iso in currency.Countries)
                    {
                        var c = await rep.CountryRepository.GetAsync(iso);

                        if (c == null)
                        {
                            return(BadRequest($"Country {iso} not found"));
                        }
                        newCurrency.Countries.Add(c);
                    }
                }

                newCurrency = rep.CurrencyRepository.Add(newCurrency);

                await rep.CompleteAsync();

                var result   = newCurrency.AsCurrencyDTO(Url);
                var response = Request.CreateResponse(HttpStatusCode.Created, result);
                response.Headers.Location = new Uri(result.GetUrl);
                return(ResponseMessage(response));
            }
        }