ASP.NET MVC5 add 2 Sync Times to the Application

49 Views Asked by At

I Added the 2 Sync Times to my DB as 2 new columns and inserted values as below:

USE [DB]


ALTER TABLE [dbo].[TableName]
    ADD ColumnName2 time, ColumnName3 time

This was for adding the columns.

For inserting the row values I did:

USE DB

INSERT INTO TableName (ColumnName2, ColumnName3)
VALUES ('20:30:00', '23:30:00')

This was the data for the fixed times in the rows of those columns.

I also went through all the layers of the application such as (controller, models, views, queries, services, interfaces, and so forth. NOW when I try to update any of the new times added they default to the first time that already existed on the table as a COLUMN with time in the row.

I could not post the image for the time fields from the application because it is not permitted. However, the image is in a little panel and consists of 3 fields (textboxfor) with a time picker for each one.

Any help would be greatly appreciated.

Thanks

Now I thought I would post some of the example code to see if this helps

// My controller method for those sync times

[HttpPost]
        [Page(PageName.UpdateSystemConfigTime)]
        public ActionResult UpdateTime(SystemMaintenanceViewModel model)
        {
            var dateTime = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime, "h:mm tt", CultureInfo.InvariantCulture);
            var dateTime2 = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime2, "h:mm tt", CultureInfo.InvariantCulture);
            var dateTime3 = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime3, "h:mm tt", CultureInfo.InvariantCulture);                        
            //model.ProcessTime
            if (model.SystemConfiguration.SynchronizationTime != null &&
                model.SystemConfiguration.SynchronizationTime2 != null &&
                model.SystemConfiguration.SynchronizationTime3 != null);
            {
                var sysConfig = new DTO.SystemSync.SystemConfiguration
                {
                    SyncTime = dateTime.TimeOfDay,
                    SyncTime2 = dateTime2.TimeOfDay,
                    SyncTime3 = dateTime3.TimeOfDay
                };


                configService.UpdateSyncTime(sysConfig);
                configService.UpdateSyncTime2(sysConfig);
                configService.UpdateSyncTime3(sysConfig);

            }


            return RedirectToAction("Index");
        }



////My Private method

private SystemConfiguration GetSystemConfig()
        {
            var model = new SystemConfiguration();
            var config = configService.GetSyncTime();
                         configService.GetSyncTime2();
                         configService.GetSyncTime3();

            if (config == null) return model;
            var ts = config.SyncTime;
            if (ts != null)
            {
                model.SynchronizationTime = ts.ToString();
            }

            var ts2 = config.SyncTime2;
            if (ts2 != null)
            {
                model.SynchronizationTime2 = ts2.ToString();
            }

            var ts3 = config.SyncTime3;
            if (ts3 != null)
            {
                model.SynchronizationTime3 = ts3.ToString();
            }
            return model;
============================================================================
/// My configuration command


namespace --.--.Commands
{
    public class ConfigurationCommand : CommandBase, IConfigurationCommand
    {
        static ConfigurationCommand()
        {
            ConfigureAutoMapper();
        }

        private static void ConfigureAutoMapper()
        {
             Mapper.CreateMap<SystemConfiguration, entity.SystemConfiguration>()
                .ForMember(dest => dest.SyncTime, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>())
                .ForMember(dest => dest.SyncTime2, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>())
                .ForMember(dest => dest.SyncTime3, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>());
        }

        public void UpdateSyncTime(SystemConfiguration timeOfDay)
        {
            Guard.NotNull(timeOfDay);
            var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);

            var config = Context.SystemConfigurations.SingleOrDefault();

            //if this is the first time, then we need to insert
            if (config == null)
            {
                var newConfig = new entity.SystemConfiguration
                {
                    SyncTime = mapped.SyncTime
                };
                Context.SystemConfigurations.Add(newConfig);
            }
            else
            {
                config.SyncTime = mapped.SyncTime;
            }
            SaveChanges();
        }

        public void UpdateSyncTime2(SystemConfiguration timeOfDay)
        {
            Guard.NotNull(timeOfDay);
            var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);

            var config = Context.SystemConfigurations.SingleOrDefault();


            if (config == null)
            {
                var newConfig = new entity.SystemConfiguration
                {
                    SyncTime2 = mapped.SyncTime2
                };
                Context.SystemConfigurations.Add(newConfig);
            }
            else
            {
                config.SyncTime2 = mapped.SyncTime2;
            }
            SaveChanges();
        }

        public void UpdateSyncTime3(SystemConfiguration timeOfDay)
        {
            Guard.NotNull(timeOfDay);
            var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);

            var config = Context.SystemConfigurations.SingleOrDefault();


            if (config == null)
            {
                var newConfig = new entity.SystemConfiguration
                {
                    SyncTime3 = mapped.SyncTime3
                };
                Context.SystemConfigurations.Add(newConfig);
            }
            else
            {
                config.SyncTime3 = mapped.SyncTime3;
            }
            SaveChanges();
        }
    }
}



=========================================================================================================
// My configuration service


namespace --.--.--.SystemSync
{
    public class ConfigurationService : IConfigurationService
    {
        private IConfigurationQuery query;
        private IConfigurationCommand command;

        public ConfigurationService(IConfigurationQuery query,IConfigurationCommand command)
        {
            this.query = query;
            this.command = command;
        }

        public void UpdateSyncTime(SystemConfiguration timeOfDay)
        {
            command.UpdateSyncTime(timeOfDay);

        }

        public void UpdateSyncTime2(SystemConfiguration timeOfDay)
        {
            command.UpdateSyncTime2(timeOfDay);
        }

        public void UpdateSyncTime3(SystemConfiguration timeOfDay)
        {
            command.UpdateSyncTime3(timeOfDay);
        }


        public SystemConfiguration GetSyncTime()
        {
            return query.GetSyncTime();
        }

        public SystemConfiguration GetSyncTime2()
        {
            return query.GetSyncTime2();
        }

        public SystemConfiguration GetSyncTime3()
        {
            return query.GetSyncTime3();
        }


        public List<PageResource> GetPages()
        {
            return query.GetPages().ToList();
        }

    }
}
1

There are 1 best solutions below

0
On

You made a comment about fixed times, Are you looking for something like this?

   CREATE TABLE [dbo].[Zamen](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [time1] [time](3) NOT NULL,
    [time2] [time](3) NOT NULL,
    [Content] [varchar](100) NULL,
 CONSTRAINT [PK_Zamen] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
))

GO

ALTER TABLE [dbo].[Zamen] ADD  CONSTRAINT [DF_Zamen_time1]  DEFAULT (getdate()) FOR [time1]
GO

ALTER TABLE [dbo].[Zamen] ADD  CONSTRAINT [DF_Zamen_time2]  DEFAULT (getdate()) FOR [time2]
GO

Those alter table statements allow the time to be automatically inserted. So when you do this:

INSERT INTO Zamen (Content) VALUES ('demo')

The current times are placed into the values.

*After seeing the code you added, some input: In your UpdateTime Action Method, a problem that stands out is you are calling UpdateTimeSync three times, but passing it all three variables each time. I would suggest to refactor your update method -- instead of three update methods, use one update method for all time variables.