Exemple #1
0
        private Rate UpdateRates(Rate _objToUpdate, RateDetailsVM dataItem, int selection)
        {
            if (dataItem == null)
            {
                return(_objToUpdate);
            }

            if (_objToUpdate == null)
            {
                _objToUpdate = new Rate();
            }

            _objToUpdate.RateCode         = dataItem.RateCode;
            _objToUpdate.RateAmount       = dataItem.RateAmount;
            _objToUpdate.RateApplications = (RateApplicationsSettings)selection;
            _objToUpdate.IsActive         = dataItem.IsActive;
            _objToUpdate.FranchiseId      = dataItem.FranchiseId;
            _objToUpdate.ActivationDate   = DateTime.Now.ToUTC();

            return(UpdateAuditTracking(_objToUpdate));
        }
Exemple #2
0
        public ActionResult SaveRate(RateDetailsVM formValues)
        {
            string _objDesc   = "Rate";
            int    _selection = 0;

            if (formValues == null)
            {
                ModelState.AddModelError(string.Empty, $"Valid {_objDesc.ToLower()} data not found.");
            }

            if (String.IsNullOrWhiteSpace(formValues.SelectedRatesJson))
            {
                ModelState.AddModelError(string.Empty, $"Valid {_objDesc.ToLower()} selections not found.");
            }
            else
            {
                List <SelectedRateItem> _selectionList = JsonConvert.DeserializeObject <List <SelectedRateItem> >(formValues.SelectedRatesJson);
                foreach (SelectedRateItem _item in _selectionList)
                {
                    _selection += _item.Id;
                }
            }

            if (ModelState.IsValid)
            {
                String _id    = formValues.Id;
                var    bIsNew = formValues.IsNewItem;

                try
                {
                    Rate _objToUpdate = null;

                    using (DBManager db = new DBManager())
                    {
                        if (bIsNew)
                        {
                            _objToUpdate = UpdateRates(null, formValues, _selection);
                            StringBuilder _sql = new StringBuilder();
                            _sql.Append("Insert into Rates (Id, CreatedAt, UpdatedAt, UpdatedBy, IsActive, RowVersion, ");
                            _sql.Append("RateCode, RateAmount, RateApplications, FranchiseId)");
                            _sql.Append(" values (");
                            _sql.Append($"'{_objToUpdate.Id}',");
                            _sql.Append($"'{_objToUpdate.CreatedAt.FormatDatabaseDateTime()}',");
                            _sql.Append($"'{_objToUpdate.UpdatedAt.FormatDatabaseDateTime()}',");
                            _sql.Append($"'{_objToUpdate.UpdatedBy}',");
                            _sql.Append($"{_objToUpdate.IsActive},");
                            _sql.Append($"'{_objToUpdate.RowVersion.FormatDatabaseDateTime()}',");
                            _sql.Append($"'{_objToUpdate.RateCode}',");
                            _sql.Append($"{_objToUpdate.RateAmount},");
                            _sql.Append($"{(int)_objToUpdate.RateApplications},");
                            _sql.Append($"'{_objToUpdate.FranchiseId}'");
                            _sql.Append(")");
                            db.getConnection().Execute(_sql.ToString());

                            //var newId = db.Insert<Rate>(_objToUpdate);
                        }
                        else
                        {
                            StringBuilder _sql = new StringBuilder();
                            _sql.Append($"Select * from Rates where id = '{_id}'");
                            _objToUpdate = db.getConnection().Query <Rate>(_sql.ToString()).SingleOrDefault();

                            if (_objToUpdate == null)
                            {
                                ModelState.AddModelError(string.Empty, $"{_objDesc} [{_id.ToString()}] not found.  Please try again.");
                                return(JsonFormResponse());
                            }

                            _objToUpdate = UpdateRates(_objToUpdate, formValues, _selection);

                            _sql.Clear();
                            _sql.Append("Update Rates set ");
                            _sql.Append($"UpdatedAt = '{_objToUpdate.UpdatedAt.FormatDatabaseDateTime()}'");
                            _sql.Append($",RowVersion = '{_objToUpdate.RowVersion.FormatDatabaseDateTime()}'");
                            _sql.Append($",UpdatedBy = '{_objToUpdate.UpdatedBy}'");
                            _sql.Append($",IsActive = {_objToUpdate.IsActive}");
                            _sql.Append($",RateCode = '{_objToUpdate.RateCode}'");
                            _sql.Append($",RateAmount = {_objToUpdate.RateAmount}");
                            _sql.Append($",RateApplications = '{(int)_objToUpdate.RateApplications}'");
                            _sql.Append($",FranchiseId = '{_objToUpdate.FranchiseId}'");
                            _sql.Append($" where Id = '{_objToUpdate.Id}'");
                            db.getConnection().Execute(_sql.ToString());
                        }

                        IAppCache cache     = new CachingService();
                        var       cacheName = "Rates";
                        if (Helpers.IsValidGuid(_objToUpdate.FranchiseId))
                        {
                            cacheName += $"_{_objToUpdate.FranchiseId.ToString()}";
                        }
                        cache.Remove(cacheName);
                    }
                    return(JsonSuccessResponse($"{_objDesc} saved successfully", _objToUpdate));
                }
                //catch (DbUpdateConcurrencyException ex)
                //{
                //	var entry = ex.Entries.Single();
                //	var clientValues = (Rate)entry.Entity;
                //	var databaseEntry = entry.GetDatabaseValues();
                //	if (databaseEntry == null)
                //	{
                //		ModelState.AddModelError(string.Empty, $"Unable to save changes. The {_objDesc.ToLower()} was deleted by another user.");
                //	}
                //	else
                //	{
                //		var databaseValues = (Rate)databaseEntry.ToObject();

                //		if (databaseValues.RateCode != clientValues.RateCode)
                //		{
                //			ModelState.AddModelError("RateCode", "Current database value for rate code: " + databaseValues.RateCode);
                //		}

                //		if (databaseValues.RateAmount != clientValues.RateAmount)
                //		{
                //			ModelState.AddModelError("RateAmount", "Current database value for rate amount: " + databaseValues.RateAmount);
                //		}

                //		ModelState.AddModelError(string.Empty, "The record you attempted to edit "
                //			+ "was modified by another user after you got the original value. The edit operation "
                //			+ "was canceled. If you still want to edit this record, click the Save button again.");
                //	}
                //}
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, Helpers.FormatModelError($"Error saving {_objDesc.ToLower()}", ex));

                    LogHelper log = new LogHelper();
                    log.Log(LogHelper.LogLevels.Error, $"Error saving {_objDesc.ToLower()}", nameof(Rate), ex, formValues, Helpers.ParseValidationErrors(ex));
                }
            }

            if (!ModelState.IsValid)
            {
                Helpers.LogFormValidationErrors(ModelState, nameof(Rate), formValues);
            }

            return(JsonFormResponse());
        }