public IActionResult StartActivity([FromBody] ActivityStartDto activity)
        {
            if (activity == null)
            {
                return(BadRequest("Activity object cannot be null"));
            }


            var newActivity = _activityService.StartActivity(activity);

            return(CreatedAtRoute("ActivityById", new { activityId = newActivity.ActivityID }, newActivity));
        }
Esempio n. 2
0
        public ActivityStartReturnDto StartActivity(ActivityStartDto activity)
        {
            // look for currently active Activity - if found, stop it and start this one
            var currentlyActive = GetCurrentlyActiveActivity();

            if (currentlyActive != null)
            {
                // TODO update with current time sent from client instead of server time
                StopActivity(new ActivityStopDto {
                    TimeEnd = DateTime.Now
                });
            }


            // create new Activity
            var entity = new Activity
            {
                Name      = activity.Name,
                TimeStart = activity.TimeStart ?? DateTime.Now
            };

            // FluentValidation already validated that if client sents ProjectID it corresponds to existing Project
            // otherwise it doesn't even fire this method
            if (activity.ProjectID != null)
            {
                var assignedProject = _context.Projects.Where(p => p.ProjectID == activity.ProjectID).SingleOrDefault();
                entity.Project = assignedProject;
            }

            _context.Activities.Add(entity);
            _context.SaveChanges();

            var activityReturnDto = new ActivityStartReturnDto
            {
                ActivityID  = entity.ActivityID,
                Name        = entity.Name,
                TimeStart   = entity.TimeStart,
                ProjectID   = entity.Project?.ProjectID,
                ProjectName = entity.Project?.Name
            };

            return(activityReturnDto);
        }