Beispiel #1
0
        public async Task <ActionResult <IEnumerable <TimesheetItem> > > Update(TimesheetItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException();
            }

            var sessionData = _sessionService.GetSession(HttpContext);

            if (sessionData == null)
            {
                return(Unauthorized());
            }

            return(Ok(await _timesheetRepository.Update(sessionData.UserId, item)));
        }
        public async Task <TimesheetItem> Update(int userId, TimesheetItem item)
        {
            ValidateTimesheetItem(item);
            User user = await GetUser(userId);

            using (var connection = await _databaseService.GetConnection())
            {
                using (var stmt = connection.GetStatement())
                {
                    stmt.SQL =
                        "UPDATE TIMESHEET " +
                        "SET WORKING_DATE=@workingDate, WORKING_HOURS=@workingHours, WORKING_DESCRIPTION=@workingDesciption " +
                        "WHERE (USER_ID=@userId OR @canManageTimesheet=1) AND TIMESHEET_ID=@timesheetId";
                    stmt.Parameters = new Dictionary <string, object>()
                    {
                        { "@workingDate", item.Date },
                        { "@workingHours", item.DurationInHours },
                        { "@workingDesciption", item.Description },
                        { "@userId", userId },
                        { "@timesheetId", item.Id },
                        { "@canManageTimesheet", user.Settings?.Role?.ManageUserTimesheet == true ? 1 : 0 }
                    };

                    var affectedRows = await stmt.Execute();

                    if (affectedRows != 1)
                    {
                        throw new Exception(Messages.CantUpdateRecord);
                    }

                    return(new TimesheetItem()
                    {
                        Id = item.Id,
                        Description = item.Description,
                        Date = item.Date,
                        DurationInHours = item.DurationInHours,
                        User = item.User
                    });
                }
            }
        }
        private void ValidateTimesheetItem(TimesheetItem item)
        {
            if (item == null)
            {
                throw new Exception(Messages.NoItemToRecord);
            }

            if (item.Date == DateTime.MinValue)
            {
                throw new Exception(Messages.TimesheetWrongDate);
            }

            if (string.IsNullOrWhiteSpace(item.Description))
            {
                throw new Exception(Messages.TimesheetWrongDescription);
            }

            if (item.DurationInHours < Constants.MinWorkingHours || item.DurationInHours > Constants.MaxWorkingHours)
            {
                throw new Exception(string.Format(Messages.TimesheetWrongWorkingHoursRange, Constants.MinWorkingHours, Constants.MaxWorkingHours));
            }
        }
        public async Task <TimesheetItem> Add(int userId, TimesheetItem item)
        {
            ValidateTimesheetItem(item);
            User user = await GetUser(userId);

            using (var connection = await _databaseService.GetConnection())
            {
                using (var stmt = connection.GetStatement())
                {
                    stmt.SQL =
                        "INSERT INTO TIMESHEET(USER_ID, WORKING_DATE, WORKING_HOURS, WORKING_DESCRIPTION) " +
                        "VALUES(@userId, @workingDate, @workingHours, @workingDesciption)";
                    stmt.Parameters = new Dictionary <string, object>()
                    {
                        { "@userId", userId },
                        { "@workingDate", item.Date },
                        { "@workingHours", item.DurationInHours },
                        { "@workingDesciption", item.Description }
                    };

                    var affectedRows = await stmt.Execute();

                    if (affectedRows != 1)
                    {
                        throw new Exception(Messages.CantUpdateRecord);
                    }

                    return(new TimesheetItem()
                    {
                        Id = connection.LastInsertRowId,
                        Description = item.Description,
                        Date = item.Date,
                        DurationInHours = item.DurationInHours,
                        User = user.Email
                    });
                }
            }
        }