Beispiel #1
0
 public void InsertUserActivityDate(UserActivityDate userActivityDate)
 {
     using (var db = this.GetDatabase())
     {
         db.UserActivityDate.Add(userActivityDate);
         db.SaveChanges();
     }
 }
Beispiel #2
0
 public void EditUserActivityDate(UserActivityDate userActivityDate)
 {
     using (var db = this.GetDatabase())
     {
         UserActivityDate userActivityDateDB = db.UserActivityDate.SingleOrDefault(usrActivityDateDB => usrActivityDateDB.Id == userActivityDate.Id);
         userActivityDateDB.IsPartecipant = userActivityDate.IsPartecipant;
         db.SaveChanges();
     }
 }
Beispiel #3
0
        public UserActivityDate GetUserActivityDateByUserAndActivityDate(ActivityDate activityDate)
        {
            UserActivityDate userActivityDate = this.User.UserActivityDate.SingleOrDefault(
                userActivityDateDB =>
                userActivityDateDB.UserId == this.User.Id &&
                userActivityDateDB.ActivityDateId == activityDate.Id
                );

            return(userActivityDate != null ? userActivityDate : new UserActivityDate());
        }
Beispiel #4
0
        public IActionResult SaveUserActivities([FromBody] SaveUserActivityModel[] models)
        {
            if (!ModelState.IsValid)
            {
                string messages = string.Join("; ", ModelState.Values
                                              .SelectMany(x => x.Errors)
                                              .Select(x => x.ErrorMessage));

                return(BadRequest(messages));
            }

            try
            {
                Guid?userId = _ctx.Users.Where(u => u.Login == HttpContext.User.Identity.Name.ToUpper()).SingleOrDefault()?.Id;

                if (null == userId || !userId.HasValue)
                {
                    userId = Guid.Empty;
                    //return BadRequest("User is not authorized");
                }

                List <UserActivityDate> userActivities = null;

                foreach (SaveUserActivityModel model in models)
                {
                    DateTime date = model.day.Date;

                    if (0 == model.activityId)
                    {
                        char[] allowed = new char[] { ',', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'H', 'V' };
                        string hours   = model.hours.Trim().Replace(',', '.').ToUpper();

                        if (!hours.All(c => allowed.Contains(c)))
                        {
                            model.hours = "0";
                        }

                        UserDateWorkHours wh = _ctx.UserDateWorkHours.SingleOrDefault(h =>
                                                                                      h.UserId == userId && h.Date == date);
                        if (null == wh)
                        {
                            _ctx.UserDateWorkHours.Add(new UserDateWorkHours
                            {
                                UserId    = userId.Value,
                                Date      = date,
                                WorkHours = hours
                            });
                        }
                        else
                        {
                            if (wh.WorkHours != hours)
                            {
                                wh.WorkHours = hours;
                            }
                        }

                        continue;
                    }

                    if (null == userActivities)
                    {
                        userActivities = _ctx.UserActivityDates
                                         .Where(ead => ead.UserActivity.UserId == userId)
                                         .Include(ead => ead.UserActivity)
                                         .ToList();
                    }

                    UserActivityDate ead = userActivities
                                           .SingleOrDefault(ea =>
                                                            ea.UserActivity.ActivityId == model.activityId && ea.Date == date);

                    if (model.count > 0)
                    {
                        if (null == ead)
                        {
                            UserActivity ea = _ctx.UserActivities
                                              .SingleOrDefault(x => x.UserId == userId && x.ActivityId == model.activityId);

                            if (null == ea)
                            {
                                ea = new UserActivity
                                {
                                    UserId     = userId.Value,
                                    ActivityId = model.activityId
                                };

                                _ctx.UserActivities.Add(ea);
                            }

                            ead = new UserActivityDate
                            {
                                UserActivity = ea, Date = date
                            };
                            _ctx.UserActivityDates.Add(ead);
                        }

                        ead.Count = model.count.Value;
                    }
                    else
                    {
                        if (null != ead)
                        {
                            _ctx.UserActivityDates.Remove(ead);
                        }
                    }
                }

                _ctx.SaveChanges();
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }

            return(Ok("Success"));
        }