/// <summary> /// The Creates entity for RentalViewModel. /// </summary> /// <param name="viewModel">The viewModel <see cref="RentalViewModel"/>.</param> /// <returns>The <see cref="SimpleResponse{RentalViewModel}"/>.</returns> public SimpleResponse <RentalViewModel> Create(RentalViewModel model) { var response = new SimpleResponse <RentalViewModel>(); try { var validation = model.Validate(); if (validation.HasError) { return(new SimpleResponse <RentalViewModel> { Data = model, ResponseCode = BusinessResponseValues.ValidationErrorResult, ResponseMessage = validation.AllValidationMessages }); } using (var context = new PublicDbContext()) { var entity = Map <RentalViewModel, Rental>(model); context.Rental.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); }
/// <summary> /// Updates entity for RentalViewModel. /// </summary> /// <param name="viewModel">The viewModel <see cref="RentalViewModel"/>.</param> /// <returns>The <see cref="SimpleResponse"/>.</returns> public SimpleResponse Update(RentalViewModel 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.Rental.SingleOrDefault(q => q.RentalId == model.RentalId); if (entity == null || entity == default(Rental)) { response.ResponseCode = BusinessResponseValues.NullEntityValue; response.ResponseMessage = "Kayýt bulunamadý."; return(response); } MapTo(model, entity); // context.Rental.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); }