Import parents
This commit is contained in:
142
process.go
142
process.go
@@ -11,9 +11,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Directory struct {
|
||||||
|
Students map[string]*Student
|
||||||
|
Parents map[string]*Parent
|
||||||
|
}
|
||||||
|
|
||||||
type Person struct {
|
type Person struct {
|
||||||
FirstName string
|
Name string
|
||||||
LastName string
|
|
||||||
Email string
|
Email string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,6 +25,11 @@ type Student struct {
|
|||||||
Person
|
Person
|
||||||
Class string
|
Class string
|
||||||
Grade string
|
Grade string
|
||||||
|
Parents []*Parent
|
||||||
|
}
|
||||||
|
|
||||||
|
type Parent struct {
|
||||||
|
Person
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -34,13 +43,20 @@ func main() {
|
|||||||
fatal(l, "please pass --directory")
|
fatal(l, "please pass --directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := loadDirectory(l, *directoryPath)
|
dir, err := loadDirectory(l, *directoryPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal(l, "failed to load directory", "error", err)
|
fatal(l, "failed to load directory", "error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
l.Info("loaded directory", "directory", dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadDirectory(l *slog.Logger, path string) (*string, error) {
|
func loadDirectory(l *slog.Logger, path string) (*Directory, error) {
|
||||||
|
d := &Directory{
|
||||||
|
Students: map[string]*Student{},
|
||||||
|
Parents: map[string]*Parent{},
|
||||||
|
}
|
||||||
|
|
||||||
fh, err := os.Open(path)
|
fh, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -74,6 +90,36 @@ func loadDirectory(l *slog.Logger, path string) (*string, error) {
|
|||||||
return nil, fmt.Errorf("'Grade' field missing")
|
return nil, fmt.Errorf("'Grade' field missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
iParent1FirstName := slices.Index(headers, "Parent 1\nFirst")
|
||||||
|
if iParent1FirstName == -1 {
|
||||||
|
return nil, fmt.Errorf("'Parent 1\\nFirst' field missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
iParent1LastName := slices.Index(headers, "Parent 1\nLast")
|
||||||
|
if iParent1LastName == -1 {
|
||||||
|
return nil, fmt.Errorf("'Parent 1\\nLast' field missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
iParent1Email := slices.Index(headers, "Parent 1\nEmail")
|
||||||
|
if iParent1Email == -1 {
|
||||||
|
return nil, fmt.Errorf("'Parent 1\\nEmail' field missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
iParent2FirstName := slices.Index(headers, "Parent 2\nFirst")
|
||||||
|
if iParent2FirstName == -1 {
|
||||||
|
return nil, fmt.Errorf("'Parent 2\\nFirst' field missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
iParent2LastName := slices.Index(headers, "Parent 2\nLast")
|
||||||
|
if iParent2LastName == -1 {
|
||||||
|
return nil, fmt.Errorf("'Parent 2\\nLast' field missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
iParent2Email := slices.Index(headers, "Parent 2 Email")
|
||||||
|
if iParent2Email == -1 {
|
||||||
|
return nil, fmt.Errorf("'Parent 2 Email' field missing")
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
row, err := r.Read()
|
row, err := r.Read()
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
@@ -87,29 +133,93 @@ func loadDirectory(l *slog.Logger, path string) (*string, error) {
|
|||||||
class = ""
|
class = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parents := []*Parent{}
|
||||||
|
|
||||||
|
parent1 := d.AddParent(
|
||||||
|
row[iParent1Email],
|
||||||
|
fmt.Sprintf("%s %s", row[iParent1FirstName], row[iParent1LastName]),
|
||||||
|
)
|
||||||
|
|
||||||
|
parents = append(parents, parent1)
|
||||||
|
|
||||||
|
if row[iParent2Email] != "" {
|
||||||
|
parent2 := d.AddParent(
|
||||||
|
row[iParent2Email],
|
||||||
|
fmt.Sprintf("%s %s", row[iParent2FirstName], row[iParent2LastName]),
|
||||||
|
)
|
||||||
|
|
||||||
|
parents = append(parents, parent2)
|
||||||
|
}
|
||||||
|
|
||||||
studentEmail := fmt.Sprintf(
|
studentEmail := fmt.Sprintf(
|
||||||
"%s.%s@heliosschool.org",
|
"%s.%s@heliosschool.org",
|
||||||
strings.ToLower(row[iStudentFirstName]),
|
strings.ToLower(row[iStudentFirstName]),
|
||||||
strings.ToLower(row[iStudentLastName]),
|
strings.ToLower(row[iStudentLastName]),
|
||||||
)
|
)
|
||||||
|
|
||||||
student := &Student{
|
d.AddStudent(
|
||||||
Person: Person{
|
studentEmail,
|
||||||
FirstName: row[iStudentFirstName],
|
fmt.Sprintf("%s %s", row[iStudentFirstName], row[iStudentLastName]),
|
||||||
LastName: row[iStudentLastName],
|
class,
|
||||||
Email: studentEmail,
|
row[iStudentGrade],
|
||||||
},
|
parents,
|
||||||
Class: class,
|
)
|
||||||
Grade: row[iStudentGrade],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Info("row", "row", student)
|
return d, nil
|
||||||
}
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func fatal(l *slog.Logger, msg string, args ...any) {
|
func fatal(l *slog.Logger, msg string, args ...any) {
|
||||||
l.Error(msg, args...)
|
l.Error(msg, args...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Directory) AddParent(email, name string) *Parent {
|
||||||
|
p := d.Parents[email]
|
||||||
|
if p != nil {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
p = &Parent{
|
||||||
|
Person: Person{
|
||||||
|
Name: name,
|
||||||
|
Email: email,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Parents[p.Email] = p
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Directory) AddStudent(email, name, class, grade string, parents []*Parent) *Student {
|
||||||
|
s := &Student{
|
||||||
|
Person: Person{
|
||||||
|
Name: name,
|
||||||
|
Email: email,
|
||||||
|
},
|
||||||
|
Class: class,
|
||||||
|
Grade: grade,
|
||||||
|
Parents: parents,
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Students[email] = s
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Person) String() string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"%s <%s>",
|
||||||
|
p.Name,
|
||||||
|
p.Email,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Student) String() string {
|
||||||
|
return s.Person.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Parent) String() string {
|
||||||
|
return p.Person.String()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user