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

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

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

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