Exemplo n.º 1
0
        /// <summary>
        /// Get Country by id
        /// </summary>
        /// <param name="id">Country id</param>
        /// <returns>Country json view model</returns>
        public IHttpActionResult Get(int id)
        {
            try
            {
                // get
                log.Debug("_countryService.GetCountry - countryId: " + id + " ");

                var country = new CountryViewModel(_countryService.GetCountry(id));

                log.Debug("_countryService.GetCountry - " + CountryViewModel.FormatCountryViewModel(country));

                log.Debug("result: 'success'");

                //return Json(country, JsonRequestBehavior.AllowGet);
                //return Content(JsonConvert.SerializeObject(country), "application/json");
                //return country;
                //return JsonConvert.SerializeObject(country);
                return(Ok(country));
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }
Exemplo n.º 2
0
        private CountryDTO Create(CountryViewModel viewModel)
        {
            try
            {
                log.Debug(CountryViewModel.FormatCountryViewModel(viewModel));

                CountryDTO country = new CountryDTO();

                // copy values
                viewModel.UpdateDTO(country, null); //RequestContext.Principal.Identity.GetUserId());

                // audit
                country.CreateBy = null; //RequestContext.Principal.Identity.GetUserId();
                country.CreateOn = DateTime.UtcNow;

                // add
                log.Debug("_countryService.AddCountry - " + CountryDTO.FormatCountryDTO(country));

                int id = _countryService.AddCountry(country);

                country.CountryId = id;

                log.Debug("result: 'success', id: " + id);

                return(country);
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }
Exemplo n.º 3
0
        private CountryDTO Update(CountryViewModel viewModel)
        {
            try
            {
                log.Debug(CountryViewModel.FormatCountryViewModel(viewModel));

                // get
                log.Debug("_countryService.GetCountry - countryId: " + viewModel.CountryId + " ");

                var existingCountry = _countryService.GetCountry(viewModel.CountryId);

                log.Debug("_countryService.GetCountry - " + CountryDTO.FormatCountryDTO(existingCountry));

                if (existingCountry != null)
                {
                    // copy values
                    viewModel.UpdateDTO(existingCountry, null); //RequestContext.Principal.Identity.GetUserId());

                    // update
                    log.Debug("_countryService.UpdateCountry - " + CountryDTO.FormatCountryDTO(existingCountry));

                    _countryService.UpdateCountry(existingCountry);

                    log.Debug("result: 'success'");
                }
                else
                {
                    log.Error("existingCountry: null, CountryId: " + viewModel.CountryId);
                }

                return(existingCountry);
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }
Exemplo n.º 4
0
        //[ValidateAntiForgeryToken]
        /// <summary>
        /// Save a list of Country
        /// </summary>
        /// <param name="viewModels">Country view models</param>
        /// <param name="id">(not used)</param>
        /// <returns>true if the operation is successfull</returns>
        public IHttpActionResult SaveList(CountryViewModel[] viewModels, int?id)
        {
            try
            {
                log.Debug("SaveList");

                if (viewModels != null)
                {
                    // save list
                    foreach (CountryViewModel viewModel in viewModels)
                    {
                        log.Debug(CountryViewModel.FormatCountryViewModel(viewModel));

                        if (viewModel.CountryId > 0)
                        {
                            var t = Update(viewModel);
                        }
                        else
                        {
                            var t = Create(viewModel);
                        }
                    }
                }
                else
                {
                    log.Error("viewModels: null");
                }

                //return Json(true);
                //return JsonConvert.SerializeObject(true);
                return(Ok(true));
            }
            catch (Exception e)
            {
                // error
                log.Error(e.ToString());

                throw;
            }
        }