Exemplo n.º 1
0
        private List <UpdateSuburbZonesVM> GetPostcodesPrivate(Guid?FranchiseId)
        {
            List <SuburbZone> _entityList = new List <SuburbZone>();

            using (DBManager db = new DBManager())
            {
                string _where = "where FranchiseId ";
                if (Helpers.IsValidGuid(FranchiseId))
                {
                    _where += " = '" + FranchiseId.Value.ToString() + "'";
                }
                else
                {
                    _where += " is null or LENGTH(FranchiseId)=0 or FranchiseId like '00000%'";
                }
                _entityList = db.getConnection().GetList <SuburbZone>(_where).ToList();
            }
            List <UpdateSuburbZonesVM> _editList = new List <UpdateSuburbZonesVM>();

            foreach (SuburbZone _item in _entityList)
            {
                var _vm = new UpdateSuburbZonesVM();
                _vm.PopulateVM(_item);
                _editList.Add(_vm);
            }

            return(_editList);
        }
Exemplo n.º 2
0
        private SuburbZone UpdateSettings(SuburbZone _objToUpdate, UpdateSuburbZonesVM dataItem)
        {
            if (dataItem == null)
            {
                return(_objToUpdate);
            }

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

            _objToUpdate.SuburbName  = dataItem.SuburbName;
            _objToUpdate.PostCode    = dataItem.PostCode;
            _objToUpdate.Zone        = dataItem.Zone;
            _objToUpdate.LinkedZones = dataItem.LinkedZones;
            _objToUpdate.FranchiseId = dataItem.FranchiseId;

            return(_objToUpdate);
        }
Exemplo n.º 3
0
        public ActionResult SavePostCodes(UpdateSuburbZonesVM formValues)
        {
            string _objDesc = "Suburb/zone";

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

            if (ModelState.IsValid)
            {
                // validate for unique suburb / zone for
                //SuburbZoneValidator validator = new SuburbZoneValidator(formValues.OtherZones);
                //ValidationResult results = validator.Validate(formValues);
                //if (!results.IsValid)
                //{
                //	ModelState.AddModelError(string.Empty, results.Errors.FirstOrDefault().ErrorMessage);
                //}
            }

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

                try
                {
                    SuburbZone _objToUpdate = null;

                    using (DBManager db = new DBManager())
                    {
                        if (bIsNew)
                        {
                            _objToUpdate = UpdateAuditTracking(UpdateSettings(null, formValues));
                            StringBuilder _sql = new StringBuilder();
                            _sql.Append("Insert into SuburbZones (Id, CreatedAt, UpdatedAt, UpdatedBy, IsActive,");
                            _sql.Append("RowVersion, SuburbName, PostCode, Zone, LinkedZones, 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.SuburbName}',");
                            _sql.Append($"'{_objToUpdate.PostCode}',");
                            _sql.Append($"'{_objToUpdate.Zone}',");
                            _sql.Append($"'{_objToUpdate.LinkedZones}',");
                            _sql.Append($"'{_objToUpdate.FranchiseId}'");
                            _sql.Append(")");

                            db.getConnection().Execute(_sql.ToString());
                            //https://github.com/ericdc1/Dapper.SimpleCRUD/blob/master/Dapper.SimpleCRUD/SimpleCRUD.cs#L320
                            //var _key = db.Insert<SuburbZone>(UpdateAuditTracking(_objToUpdate));
                        }
                        else
                        {
                            StringBuilder _sql = new StringBuilder();
                            _sql.Append($"Select * from SuburbZones where Id = '{_id}'");
                            _objToUpdate = db.getConnection().Query <SuburbZone>(_sql.ToString()).SingleOrDefault();

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

                            _objToUpdate = UpdateAuditTracking(UpdateSettings(_objToUpdate, formValues));
                            _sql.Clear();
                            _sql.Append("Update SuburbZones 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($",SuburbName = '{_objToUpdate.SuburbName}'");
                            _sql.Append($",PostCode = '{_objToUpdate.PostCode}'");
                            _sql.Append($",Zone = '{_objToUpdate.Zone}'");
                            _sql.Append($",LinkedZones = '{_objToUpdate.LinkedZones}'");
                            _sql.Append($",FranchiseId = '{_objToUpdate.FranchiseId}'");
                            _sql.Append($" where Id = '{_objToUpdate.Id}'");
                            db.getConnection().Execute(_sql.ToString());
                        }

                        IAppCache cache     = new CachingService();
                        var       cacheName = "Postcodes";
                        if (Helpers.IsValidGuid(_objToUpdate.FranchiseId))
                        {
                            cacheName += $"_{_objToUpdate.FranchiseId}";
                        }
                        cache.Remove(cacheName);
                        cache.Remove($"SuburbNames");
                    }

                    return(JsonSuccessResponse($"{_objDesc} saved successfully", _objToUpdate));
                }
                //catch (DbUpdateConcurrencyException ex)
                //{
                //	var entry = ex.Entries.Single();
                //	var clientValues = (SuburbZone)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 = (SuburbZone)databaseEntry.ToObject();

                //		if (databaseValues.SuburbName != clientValues.SuburbName)
                //		{
                //			ModelState.AddModelError("SuburbName", "Current database value for suburb name: " + databaseValues.SuburbName);
                //		}

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

                //		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(SavePostCodes), ex, formValues, Helpers.ParseValidationErrors(ex));
                }
            }

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

            return(JsonFormResponse());
        }