public IActionResult Update([FromBody] TimeSheetActivityUpdateModel model)
        {
            try
            {
                string ipAddress = _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();

                model.SetIpAddress(ipAddress);

                return(Ok(_activitiesMethods.Update(model)));
            }
            catch (ClientException ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new { message = ex.Message }));
            }
        }
        public TimeSheetActivity Update(TimeSheetActivityUpdateModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("activity is null*");
            }

            if (string.IsNullOrEmpty(model.message))
            {
                throw new ClientException("message is required*");
            }

            if (model.toDate <= model.fromDate)
            {
                throw new ClientException("To Date should be greater than From Date*");
            }

            // can be null (case VPN, etc..)
            string ipAddress = model.GetIpAddress();

            // clean
            model.message = model.message.Trim();

            // save the location if doesn't exist
            if (ipAddress != null)
            {
                if (ipAddress == "::1")
                {
                    ipAddress = "127.0.0.1";
                }

                _ipAddressMethods.AddIfNotExist(ipAddress);
            }


            var dbActivity = db.TimeSheetActivities
                             .Include(k => k.IpAddress)
                             .FirstOrDefault(k => k.ID == model.id);

            if (dbActivity == null)
            {
                throw new ClientException("activity not found");
            }

            string userId = db.TimeSheets.First(k => k.TimeSheetTasks.Any(t => t.ID == dbActivity.TimeSheetTaskId)).UserId;

            DataSets.TimeSheetActivity conflictedActivity = db.TimeSheetActivities.FirstOrDefault(c =>
                                                                                                  c.TimeSheetTask.TimeSheet.UserId == userId &&
                                                                                                  c.ID != dbActivity.ID &&
                                                                                                  !c.DeletedAt.HasValue &&
                                                                                                  ((model.fromDate >= c.FromDate && model.fromDate <= c.ToDate) ||
                                                                                                   (model.toDate >= c.FromDate && model.toDate <= c.ToDate) ||
                                                                                                   (!c.ToDate.HasValue && model.fromDate >= c.FromDate) ||
                                                                                                   (!c.ToDate.HasValue && model.toDate >= c.FromDate)
                                                                                                  ));

            if (conflictedActivity != null)
            {
                throw new ClientException($"An activity is already made on the choosen dates. " +
                                          $"Activity No.{conflictedActivity.ID} ({conflictedActivity.FromDate.ToDisplayDateTime()} - {(conflictedActivity.ToDate.HasValue ? conflictedActivity.ToDate.ToDisplayDateTime() : "*")})");
            }

            // check for change
            bool fromDateChanged = dbActivity.FromDate != model.fromDate;
            bool toDateChanged   = dbActivity.ToDate != model.toDate;
            bool messageChanged  = dbActivity.Message != model.message;

            bool hasChange = fromDateChanged || toDateChanged || messageChanged;

            if (hasChange)
            {
                db.TimeSheetActivityLogs.Add(new DataSets.TimeSheetActivityLog()
                {
                    Address             = dbActivity.Address,
                    FromDate            = dbActivity.FromDate,
                    ToDate              = dbActivity.ToDate,
                    Message             = dbActivity.Message,
                    TimeSheetActivityId = dbActivity.ID,
                    DateAdded           = DateTime.Now
                });

                // update the activity
                dbActivity.Address  = ipAddress;
                dbActivity.Message  = model.message;
                dbActivity.FromDate = model.fromDate;
                dbActivity.ToDate   = model.toDate;

                db.SaveChanges();
            }

            return(_mapper.Map <TimeSheetActivity>(dbActivity));
        }