<commit_before>package application

import (
	"github.com/carbocation/interpose"
	gorilla_mux "github.com/gorilla/mux"
	"github.com/gorilla/sessions"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
	"github.com/sendgrid/sendgrid-go"
	"github.com/sendgrid/sendgrid-go/helpers/mail"
	"github.com/spf13/viper"
	"net/http"
	"os"
	"time"

	"github.com/jpatrickpark/server1/handlers"
	"github.com/jpatrickpark/server1/middlewares"
	"github.com/jpatrickpark/server1/models"
)

func SendCourseOpenEmail(courseCode, quarter, email string) {
	from := mail.NewEmail("My UCI Class Is Full", "<EMAIL>")
	to := mail.NewEmail(email, email)
	title := "Your course " + courseCode + " is available!"
	content := "<p>Your course " + courseCode + " for " + handlers.Readable(quarter) + " quarter is available!</p><p>Go ahead and enroll in now on <a href='https://www.reg.uci.edu'>Webreg</a>!</p>"
	newContent := mail.NewContent("text/html", content)
	message := mail.NewV3MailInit(from, title, to, newContent)
	request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
	request.Method = "POST"
	request.Body = mail.GetRequestBody(message)
	// I should think more about what to do when the email fails to send.
	sendgrid.API(request)
}
func SendToAccordingUsers(db *sqlx.DB, courseId int64, courseCode, quarter string) {
	pair := models.NewUserCoursePair(db)
	userStruct := models.NewUser(db)
	pairs, err1 := pair.GetPairsByCourseId(nil, courseId)
	if err1 == nil {
		for _, item := range *pairs {
			user, err2 := userStruct.GetById(nil, item.UserID)
			if err2 == nil {
				SendCourseOpenEmail(courseCode, quarter, user.Email)
			}
		}
	}
}
func My_uci_class_is_full(db *sqlx.DB) {
	for {
		course := models.NewCourse(db)
		courses, err := course.AllCourses(nil)
		if err == nil {
			now := time.Now()
			//now = time.Date(2016, time.March, 10, 23, 0, 0, 0, time.UTC)
			possibleQuarters := handlers.PossibleQuarters(now)
			for _, item := range courses {
				if handlers.Contains(possibleQuarters, item.Quarter) {
					newStatus := handlers.CourseStatus(item.Quarter, item.CourseCode)
					if item.Status != newStatus {
						course.UpdateCourse(nil, item.ID, newStatus)
						if item.Status == models.FULL && (newStatus == models.OPEN || newStatus == models.WAITLIST) {
							go SendToAccordingUsers(db, item.ID, item.CourseCode, item.Quarter)
						}
					}
				}
			}
		}
		time.Sleep(time.Minute)
	}
}

// New is the constructor for Application struct.
func New(config *viper.Viper) (*Application, error) {
	dsn := config.Get("dsn").(string)

	db, err := sqlx.Connect("postgres", dsn)
	if err != nil {
		return nil, err
	}
	go My_uci_class_is_full(db)

	cookieStoreSecret := config.Get("cookie_secret").(string)<commit_msg>separated function call to My_uci_class_is_full(db) into separate program<commit_after>package application

import (
	"github.com/carbocation/interpose"
	gorilla_mux "github.com/gorilla/mux"
	"github.com/gorilla/sessions"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
	"github.com/sendgrid/sendgrid-go"
	"github.com/sendgrid/sendgrid-go/helpers/mail"
	"github.com/spf13/viper"
	"net/http"
	"os"
	"time"

	"github.com/jpatrickpark/server1/handlers"
	"github.com/jpatrickpark/server1/middlewares"
	"github.com/jpatrickpark/server1/models"
)

func SendCourseOpenEmail(courseCode, quarter, email string) {
	from := mail.NewEmail("My UCI Class Is Full", "<EMAIL>")
	to := mail.NewEmail(email, email)
	title := "Your course " + courseCode + " is available!"
	content := "<p>Your course " + courseCode + " for " + handlers.Readable(quarter) + " quarter is available!</p><p>Go ahead and enroll in now on <a href='https://www.reg.uci.edu'>Webreg</a>!</p>"
	newContent := mail.NewContent("text/html", content)
	message := mail.NewV3MailInit(from, title, to, newContent)
	request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
	request.Method = "POST"
	request.Body = mail.GetRequestBody(message)
	// I should think more about what to do when the email fails to send.
	sendgrid.API(request)
}
func SendToAccordingUsers(db *sqlx.DB, courseId int64, courseCode, quarter string) {
	pair := models.NewUserCoursePair(db)
	userStruct := models.NewUser(db)
	pairs, err1 := pair.GetPairsByCourseId(nil, courseId)
	if err1 == nil {
		for _, item := range *pairs {
			user, err2 := userStruct.GetById(nil, item.UserID)
			if err2 == nil {
				SendCourseOpenEmail(courseCode, quarter, user.Email)
			}
		}
	}
}
func My_uci_class_is_full(db *sqlx.DB) {
	for {
		course := models.NewCourse(db)
		courses, err := course.AllCourses(nil)
		if err == nil {
			now := time.Now()
			//now = time.Date(2016, time.March, 10, 23, 0, 0, 0, time.UTC)
			possibleQuarters := handlers.PossibleQuarters(now)
			for _, item := range courses {
				if handlers.Contains(possibleQuarters, item.Quarter) {
					newStatus := handlers.CourseStatus(item.Quarter, item.CourseCode)
					if item.Status != newStatus {
						course.UpdateCourse(nil, item.ID, newStatus)
						if item.Status == models.FULL && (newStatus == models.OPEN || newStatus == models.WAITLIST) {
							go SendToAccordingUsers(db, item.ID, item.CourseCode, item.Quarter)
						}
					}
				}
			}
		}
		time.Sleep(time.Minute)
	}
}

// New is the constructor for Application struct.
func New(config *viper.Viper) (*Application, error) {
	dsn := config.Get("dsn").(string)

	db, err := sqlx.Connect("postgres", dsn)
	if err != nil {
		return nil, err
	}
	

	cookieStoreSecret := config.Get("cookie_secret").(string)
{'fraction_non_alphanumeric': 0.14905490549054906, 'fraction_numerical': 0.00864086408640864, 'mean_word_length': 4.197380729653882, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3984570', 'n_tokens_mistral': 2191, 'n_tokens_neox': 1979, 'n_words': 408}