예제 #1
0
        /// <summary>
        /// Get District by id
        /// </summary>
        /// <param name="id">District id</param>
        /// <returns>District json view model</returns>
        public IHttpActionResult Get(int id)
        {
            try
            {
                // get
                log.Debug("_districtService.GetDistrict - districtId: " + id + " ");

                var district = new DistrictViewModel(_districtService.GetDistrict(id));

                log.Debug("_districtService.GetDistrict - " + DistrictViewModel.FormatDistrictViewModel(district));

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

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

                throw;
            }
        }
예제 #2
0
        private DistrictDTO Create(DistrictViewModel viewModel)
        {
            try
            {
                log.Debug(DistrictViewModel.FormatDistrictViewModel(viewModel));

                DistrictDTO district = new DistrictDTO();

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

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

                // add
                log.Debug("_districtService.AddDistrict - " + DistrictDTO.FormatDistrictDTO(district));

                int id = _districtService.AddDistrict(district);

                district.DistrictId = id;

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

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

                throw;
            }
        }
예제 #3
0
        private DistrictDTO Update(DistrictViewModel viewModel)
        {
            try
            {
                log.Debug(DistrictViewModel.FormatDistrictViewModel(viewModel));

                // get
                log.Debug("_districtService.GetDistrict - districtId: " + viewModel.DistrictId + " ");

                var existingDistrict = _districtService.GetDistrict(viewModel.DistrictId);

                log.Debug("_districtService.GetDistrict - " + DistrictDTO.FormatDistrictDTO(existingDistrict));

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

                    // update
                    log.Debug("_districtService.UpdateDistrict - " + DistrictDTO.FormatDistrictDTO(existingDistrict));

                    _districtService.UpdateDistrict(existingDistrict);

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

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

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

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

                        if (viewModel.DistrictId > 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;
            }
        }