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

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

                using (var context = new PublicDbContext())
                {
                    var entity = Map <CountryViewModel, Country>(model);
                    context.Country.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);
        }
Exemple #2
0
        /// <summary>
        /// Updates entity for CountryViewModel.
        /// </summary>
        /// <param name="viewModel">The viewModel <see cref="CountryViewModel"/>.</param>
        /// <returns>The <see cref="SimpleResponse"/>.</returns>
        public SimpleResponse Update(CountryViewModel 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.Country.SingleOrDefault(q => q.CountryId == model.CountryId);
                    if (entity == null || entity == default(Country))
                    {
                        response.ResponseCode    = BusinessResponseValues.NullEntityValue;
                        response.ResponseMessage = "Kayýt bulunamadý.";
                        return(response);
                    }

                    MapTo(model, entity);
                    // context.Country.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);
        }