예제 #1
0
        public async Task <IActionResult> Post([FromBody] MVMMonitorSave value)
        {
            if (string.IsNullOrEmpty(value.Name))
            {
                return(Error("Name is required"));
            }


            var monitorObject = new MVDMonitor
            {
                Id          = Guid.NewGuid(),
                Name        = value.Name,
                CreatedDate = DateTime.UtcNow,
                UserId      = _userId
            };

            db.Add(monitorObject);

            var monitorStepData = new MVDSMonitorStepSettingsRequest
            {
                Url = value.Url
            };
            var monitorStep = new MVDMonitorStep
            {
                Id        = Guid.NewGuid(),
                MonitorId = monitorObject.Id,
                Type      = MVDMonitorStepTypes.Request,
                Settings  = JsonConvert.SerializeObject(monitorStepData) //gelen stringi JSON' a çevir
            };

            db.MonitorSteps.Add(monitorStep);

            var result = await db.SaveChangesAsync();

            if (result > 0)
            {
                return(Success("Monitoring Save successfully",
                               new
                {
                    Id = monitorObject.Id
                }));
            }
            else
            {
                return(Error("something went wrong"));
            }
        }
예제 #2
0
        public async Task <IActionResult> Post([FromBody] MVMMonitorSave value)
        {
            if (string.IsNullOrEmpty(value.Name))
            {
                return(Error("Name is required."));
            }

            var monitorCheck = await Db.Monitors.AnyAsync(
                x => x.MonitorId != value.Id &&
                x.Name.Equals(value.Name) &&
                x.UserId == UserId);

            if (monitorCheck)
            {
                return(Error("This project name is already in use. Please choose a different name."));
            }

            MVDMonitor data = null;

            if (value.Id != Guid.Empty)
            {
                data = await Db.Monitors.FirstOrDefaultAsync(x => x.MonitorId == value.Id && x.UserId == UserId);

                if (data == null)
                {
                    return(Error("Monitor not found."));
                }

                data.UpdatedDate = DateTime.UtcNow;
                data.Name        = value.Name;
            }
            else
            {
                data = new MVDMonitor
                {
                    MonitorId   = Guid.NewGuid(),
                    CreatedDate = DateTime.UtcNow,
                    Name        = value.Name,
                    UserId      = UserId
                };
                Db.Monitors.Add(data);
            }

            var monitorStepData = new MVDSMonitorStepSettingsRequest
            {
                Url = value.Url
            };

            var step = await Db.MonitorSteps.FirstOrDefaultAsync(x => x.MonitorId == data.MonitorId && x.Type == MVDMonitorStepTypes.Request);

            if (step != null)
            {
                var requestSettings = step.SettingsAsRequest() ?? new MVDSMonitorStepSettingsRequest();
                requestSettings.Url = value.Url;
                step.Settings       = JsonConvert.SerializeObject(requestSettings);
            }
            else
            {
                step = new MVDMonitorStep
                {
                    MonitorStepId = Guid.NewGuid(),
                    Type          = MVDMonitorStepTypes.Request,
                    MonitorId     = data.MonitorId,
                    Settings      = JsonConvert.SerializeObject(monitorStepData),
                    Interval      = 10
                };
                Db.MonitorSteps.Add(step);
            };

            var result = await Db.SaveChangesAsync();

            if (result > 0)
            {
                return(Success("Monitoring saved successfully.", new
                {
                    Id = data.MonitorId
                }));
            }
            else
            {
                return(Error("Something is wrong with your model."));
            }
        }