示例#1
0
        /// <summary>
        /// The Creates entity for CityViewModel.
        /// </summary>
        /// <param name="viewModel">The viewModel <see cref="CityViewModel"/>.</param>
        /// <returns>The <see cref="SimpleResponse{CityViewModel}"/>.</returns>
        public SimpleResponse <CityViewModel> Create(CityViewModel model)
        {
            var response = new SimpleResponse <CityViewModel>();

            try
            {
                var validation = model.Validate();
                if (validation.HasError)
                {
                    return(new SimpleResponse <CityViewModel>
                    {
                        Data = model,
                        ResponseCode = BusinessResponseValues.ValidationErrorResult,
                        ResponseMessage = validation.AllValidationMessages
                    });
                }

                using (var context = new PublicDbContext())
                {
                    var entity = Map <CityViewModel, City>(model);
                    context.City.Add(entity);
                    response.ResponseCode = context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                response.ResponseCode    = BusinessResponseValues.InternalError;
                response.ResponseMessage = "Ekleme iþleminde hata oluþtu.";
                DayLogger.Error(ex);
            }

            return(response);
        }
示例#2
0
        /// <summary>
        /// Updates entity for CityViewModel.
        /// </summary>
        /// <param name="viewModel">The viewModel <see cref="CityViewModel"/>.</param>
        /// <returns>The <see cref="SimpleResponse"/>.</returns>
        public SimpleResponse Update(CityViewModel model)
        {
            var response = new SimpleResponse();

            try
            {
                var validation = model.Validate();
                if (validation.HasError)
                {
                    return(new SimpleResponse
                    {
                        ResponseCode = BusinessResponseValues.ValidationErrorResult,
                        ResponseMessage = validation.AllValidationMessages
                    });
                }

                using (var context = new PublicDbContext())
                {
                    var entity = context.City.SingleOrDefault(q => q.CityId == model.CityId);
                    if (entity == null || entity == default(City))
                    {
                        response.ResponseCode    = BusinessResponseValues.NullEntityValue;
                        response.ResponseMessage = "Kayýt bulunamadý.";
                        return(response);
                    }

                    MapTo(model, entity);
                    // context.City.Attach(entity);
                    // context.Entry(entity).State = EntityState.Modified;
                    response.ResponseCode = context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                response.ResponseCode    = BusinessResponseValues.InternalError;
                response.ResponseMessage = "Güncelleme iþleminde hata oluþtu.";
                DayLogger.Error(ex);
            }

            return(response);
        }