Created
May 14, 2024 13:08
-
-
Save Sup3r-Us3r/41954013cc80940124df30ea96f82507 to your computer and use it in GitHub Desktop.
Example in Golang for obtaining available appointments, dealing with possible conflicts between existing appointments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
type Appointment struct { | |
Start time.Time | |
Duration time.Duration | |
} | |
func findAvailableSlots(serviceDuration time.Duration) []time.Time { | |
// É necessário gerar 7 dias de horários disponíveis, para isso deve usar a data atual e ir incrementando +1 dia, é usado a data atual pois não será possível voltar no tempo e gerar horários no passado. | |
startTime := time.Date(2023, 1, 1, 8, 0, 0, 0, time.Local) | |
endTime := time.Date(2023, 1, 1, 18, 0, 0, 0, time.Local) | |
var availableSlots []time.Time | |
for startTime.Before(endTime.Add(-serviceDuration)) { // Lembrar de colocar aqui a duração mínina do serviço | |
slotEndTime := startTime.Add(serviceDuration) // Lembrar de colocar aqui a duração mínina do serviço | |
slot := Appointment{ | |
Start: startTime, | |
Duration: serviceDuration, | |
} | |
if !hasConflict(slot) { | |
availableSlots = append(availableSlots, startTime) | |
} | |
startTime = slotEndTime | |
} | |
return availableSlots | |
} | |
func hasConflict(appointment Appointment) bool { | |
// Verificar se há conflito com agendamentos existentes (exemplo) | |
existingAppointments := getExistingAppointments() | |
// appointment.Start = 09:00 | |
// appointment.Duration = 30min | |
// existing.Start = 10:00 | |
// existing.Duration = 20min | |
// 9h vem antes que 10:20 ? SIM -- O horário que inicia meu agendamento vem antes do término de um agendamento já existente? | |
// 09:30 vem depois que 10h ? NÃO -- O horário de término do meu agendamento conflita com um horário em andamento? (pelo fato do meu horário de término vir depois do horário de início de um agendamento já em andamento!) | |
for _, existing := range existingAppointments { | |
if appointment.Start.Before(existing.Start.Add(existing.Duration)) && | |
appointment.Start.Add(appointment.Duration).After(existing.Start) { | |
return true | |
} | |
} | |
return false | |
} | |
func getExistingAppointments() []Appointment { | |
// Obter agendamentos existentes do banco de dados ou de alguma outra fonte | |
// Neste exemplo, retornaremos agendamentos fixos para fins ilustrativos | |
existingAppointments := []Appointment{ | |
{ | |
Start: time.Date(2023, 1, 1, 8, 30, 0, 0, time.Local), | |
Duration: time.Minute * 60, | |
}, | |
{ | |
Start: time.Date(2023, 1, 1, 10, 0, 0, 0, time.Local), | |
Duration: time.Minute * 20, | |
}, | |
{ | |
Start: time.Date(2023, 1, 1, 10, 30, 0, 0, time.Local), | |
Duration: time.Minute * 20, | |
}, | |
{ | |
Start: time.Date(2023, 1, 1, 11, 0, 0, 0, time.Local), | |
Duration: time.Minute * 30, | |
}, | |
{ | |
Start: time.Date(2023, 1, 1, 12, 10, 0, 0, time.Local), | |
Duration: time.Minute * 50, | |
}, | |
{ | |
Start: time.Date(2023, 1, 1, 15, 0, 0, 0, time.Local), | |
Duration: time.Minute * 30, | |
}, | |
} | |
return existingAppointments | |
} | |
func main() { | |
serviceDuration := time.Minute * 30 | |
availableSlots := findAvailableSlots(serviceDuration) | |
fmt.Println("Horários disponíveis:") | |
for _, slot := range availableSlots { | |
fmt.Println(slot.Format("15:04")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment