Esempio n. 1
0
        public async Task <string> UpdatePosition(string selectedPositionName, string newNameForPosition, string newColorForPosition)
        {
            bool positionAlreadyExists = await _context.Positions.AnyAsync(p => string.Equals(newNameForPosition, p.Name));

            bool noPositionNameEntered = string.IsNullOrWhiteSpace(newNameForPosition);

            if (positionAlreadyExists)
            {
                return($"Error: A position with that name already exists.");
            }
            else if (noPositionNameEntered)
            {
                return($"Error: You must enter a name for the position.");
            }

            var selectedPosition = await _context.Positions.FirstOrDefaultAsync(p => p.Name == selectedPositionName);

            if (selectedPosition != null)
            {
                string originalName = selectedPosition.Name;

                _context.Update(selectedPosition);

                selectedPosition.Name  = newNameForPosition;
                selectedPosition.Color = newColorForPosition;

                await _context.SaveChangesAsync();

                return($"You successfully updated {originalName} to {selectedPosition.Name}.");
            }
            return($"Error: You must select a position.");
        }
        public async Task <PunchClockResult> PunchClock(int userId, int position)
        {
            try
            {
                var volunteer = await _context.VolunteerProfiles.FirstOrDefaultAsync(v => v.UserID == userId);

                if (volunteer.ApprovalStatus != ApprovalStatus.Approved)
                {
                    return(new PunchClockResult
                    {
                        Message = $"Your application must be approved before you can clock in.",
                        Success = false
                    });
                }

                var existingClockIn = await _context.ClockedTime.FirstOrDefaultAsync(ct => ct.Volunteer.UserID == userId &&
                                                                                     ct.EndTime == null);

                if (existingClockIn != null)
                {
                    _context.Update(existingClockIn);
                    existingClockIn.EndTime = DateTime.UtcNow.AddHours(-6);
                    await _context.SaveChangesAsync();

                    return(new PunchClockResult
                    {
                        Message = $"Goodbye {existingClockIn.Volunteer.FirstName}! You have successfully clocked out.",
                        Success = true
                    });
                }
                else
                {
                    ClockedTime newClockIn = new ClockedTime()
                    {
                        Volunteer = volunteer,
                        StartTime = DateTime.UtcNow.AddHours(-6),
                        Position  = await _context.Positions.FirstOrDefaultAsync(p => p.Id == position)
                    };
                    await _context.AddAsync(newClockIn);

                    await _context.SaveChangesAsync();

                    return(new PunchClockResult
                    {
                        Message = $"Welcome {newClockIn.Volunteer.FirstName}! You have successfully clocked in.",
                        Success = true
                    });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(new PunchClockResult
                {
                    Message = $"Something went wrong, please try again.",
                    Success = false
                });
            }
        }