Beispiel #1
0
//api call for submitting project
func submitProjectHandler(w http.ResponseWriter, r *http.Request) {
	//data := setDefaultData(w, r)
	//submission := make(map[string]interface{})
	// **TOP
	s := redis_session.Session(w, r)
	currentUser := user.FindUser(s.Get("Email"))

	r.ParseForm()
	castsAttendees := r.Form["castEmail[]"]
	castRoles := r.Form["castRole[]"]

	var castContainer []*project.Cast
	var newCast *project.Cast
	for i := 0; i < len(castsAttendees); i++ {
		castId := bson.NewObjectId()
		castUser := user.FindUser(castsAttendees[i])
		newCast = project.NewCast(castUser, castRoles[i], castId)
		project.InsertCast(newCast)
		castContainer = append(castContainer, newCast)
	}

	projectId := bson.NewObjectId()
	fmt.Println("ID :::: ", projectId)
	//s := redis_session.Session(w, r)
	newProject := project.NewProject(r.FormValue("title"), r.FormValue("url"), r.FormValue("shortDescription"), r.FormValue("description"), castContainer, currentUser, projectId)
	//fmt.Println("new project: ", newProject.Id)
	project.InsertProject(newProject)

	urlParts := []string{"/projects/?id=", projectId.Hex()}
	url := strings.Join(urlParts, "")
	// redirect to project page
	http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
Beispiel #2
0
func submitContestProjectHandler(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	currentUser := user.FindUser(s.Get("Email"))

	r.ParseForm()
	castsAttendees := r.Form["castEmail[]"]
	castRoles := r.Form["castRole[]"]

	newContest := role.FindContests()
	currentContest := newContest[0]

	var castContainer []*project.Cast
	var newCast *project.Cast
	for i := 0; i < len(castsAttendees); i++ {
		castId := bson.NewObjectId()
		castUser := user.FindUser(castsAttendees[i])
		newCast = project.NewCast(castUser, castRoles[i], castId)
		project.InsertCast(newCast)
		castContainer = append(castContainer, newCast)
	}

	description := []string{r.FormValue("teamName") + ": \n", r.FormValue("description")}
	combinedDescription := strings.Join(description, "")

	projectId := bson.NewObjectId()
	newProject := project.NewContestProject(r.FormValue("title"), r.FormValue("url"), r.FormValue("shortDescription"), combinedDescription, castContainer, currentUser, projectId, currentContest)
	project.InsertProject(newProject)

	urlParts := []string{"/projects/?id=", projectId.Hex()}
	url := strings.Join(urlParts, "")
	http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
Beispiel #3
0
func updateProjectHandler(w http.ResponseWriter, r *http.Request) {
	//s := redis_session.Session(w, r)
	r.ParseForm()
	//currentUser := user.FindUser(s.Get("Email"))
	castsAttendees := r.Form["castEmail[]"]
	//fmt.Println(castsAttendees)
	castRoles := r.Form["castRole[]"]
	//fmt.Println(castRoles)
	projectId := r.URL.Query().Get("id")
	var castContainer []*project.Cast
	var newCast *project.Cast
	for i := 0; i < len(castsAttendees); i++ {
		castId := bson.NewObjectId()
		//fmt.Println(castId)
		castUser := user.FindUser(castsAttendees[i])
		//fmt.Println(castUser)
		newCast = project.NewCast(castUser, castRoles[i], castId)
		project.InsertCast(newCast)
		castContainer = append(castContainer, newCast)
	}
	editedProject := project.ChangedProject(r.FormValue("title"), r.FormValue("url"), r.FormValue("shortDescription"), r.FormValue("description"), castContainer)
	project.UpdateProject(projectId, editedProject)

	urlParts := []string{"/projects/?id=", projectId, "#success"}
	url := strings.Join(urlParts, "")
	http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
Beispiel #4
0
func submitTeamHandler(w http.ResponseWriter, r *http.Request) {
	//data := setDefaultData(w, r)
	//submission := make(map[string]interface{})
	// **TOP
	r.ParseForm()
	teamMembers := r.Form["teamEmails"]

	newContest := role.FindContests()
	currentContest := role.FindContest(newContest[0].Id.Hex())

	teamContainer := make([]*user.User, 0)
	for i := 0; i < len(teamMembers); i++ {
		newUser := user.FindUser(teamMembers[i])
		teamContainer = append(teamContainer, newUser)
	}

	teamId := bson.NewObjectId()
	//s := redis_session.Session(w, r)

	// ****** CLEANING ********
	newTeam := role.NewTeam(teamContainer, r.FormValue("teamName"), r.FormValue("motto"), teamId)

	role.InsertNewTeam(newTeam)

	currentContest.InsertTeam(newTeam)

	//data["contests"] = currentContest
	http.Redirect(w, r, "/teams/browse", http.StatusTemporaryRedirect)
}
Beispiel #5
0
func deleteProjectHandler(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	currentUser := user.FindUser(s.Get("Email"))
	projectID := r.URL.Query().Get("id")
	selected := project.FindProject(projectID)
	project.DeleteProject(selected)
	urlParts := []string{"/projects/edit/?id=", currentUser.Id.Hex()}
	url := strings.Join(urlParts, "")
	http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
Beispiel #6
0
//Submits an audition in auditions/{auditionid}
func submitAuditionHandler(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	fmt.Println(r.Form)
	err := r.ParseMultipartForm(32 << 20)
	if err != nil {
		log.Printf("%s", err)
	}

	roleID := r.FormValue("id")
	fmt.Println(r.Form)
	file, handler, err := r.FormFile("auditionFile")
	defer file.Close()
	if err != nil {
		fmt.Printf("err opening audition file: %s", err)
		return
	}

	bytes, err := file.Seek(0, 2)
	if err != nil {
		panic(err)
	}

	var kilobytes int64
	kilobytes = (bytes / 1024)

	var megabytes float64
	megabytes = (float64)(kilobytes / 1024)

	if megabytes < 6 {
		attachmentURL := "/auditions/" + roleID + "/" + s.Get("Email") + "/" + handler.Filename

		uploader := s3manager.NewUploader(session.New())
		result, err := uploader.Upload(&s3manager.UploadInput{
			Body:   file,
			Bucket: aws.String("coaud"),
			Key:    aws.String(attachmentURL),
		})

		if err != nil {
			log.Fatalln("Failed to upload", err)
		}

		log.Println("Successfully uploaded to", result.Location)

		//create a new audition and add the link
		auditionID := bson.NewObjectId()
		audition := role.NewAudition(user.FindUser(s.Get("Email")), handler.Filename, result.Location, auditionID)
		curRole := role.FindRole(roleID)
		role.InsertAudition(audition, curRole)

		w.Write([]byte("uploaded"))
	} else {
		w.Write([]byte("rejected"))
	}
}
Beispiel #7
0
func teamHandler(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	currentUser := user.FindUser(s.Get("Email"))
	data := setDefaultData(w, r)
	teamID := r.URL.Query().Get("id")
	//fmt.Println(teamID)
	team := role.FindTeam(teamID)
	fmt.Println(team)
	data["team"] = team
	data["user"] = currentUser
	display(w, "viewTeam", &Page{Title: "Team", Data: data})
}
Beispiel #8
0
func setDefaultData(w http.ResponseWriter, r *http.Request) map[string]interface{} {
	data := make(map[string]interface{})
	s := redis_session.Session(w, r)
	currentUser := user.FindUser(s.Get("Email"))

	data["currentUser"] = currentUser
	if currentUser != nil {
		s.Set("DisplayName", currentUser.Email)
		s.Set("Email", currentUser.Email)
		s.Set("ID", currentUser.Id.Hex())
		s.Set("ProfilePictureURL", currentUser.ProfilePictureURL)
	}
	return data
}
Beispiel #9
0
func submitCommentHandler(w http.ResponseWriter, r *http.Request, collection string, recentOrder bool) {
	s := redis_session.Session(w, r)
	currentUser := user.FindUser(s.Get("Email"))

	message := r.FormValue("content")
	id := r.FormValue("id")

	commentID := bson.NewObjectId()
	newComment := role.NewComment(currentUser, message, commentID)

	role.InsertComment(newComment, collection, id, recentOrder)

	w.Write([]byte(`<li class="comment-posted media"><div class="media-left"><a href="/profile/?id=` + currentUser.Id.Hex() + `"><img class="img-profile media-object img-circle" src="` + currentUser.ProfilePictureURL + `"></a></div><div class="media-body"><a href="/profile/?id=` + currentUser.Id.Hex() + `"><h4 class="media-heading">` + currentUser.DisplayName + `</h4></a><span class="timestamp">` + newComment.TimeStamp.Format("Jan 02, 2006") + `</span><p class="comment-message">` + message + `</p></div></li>`))
}
Beispiel #10
0
func projectHandler(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	currentUser := user.FindUser(s.Get("Email"))
	data := setDefaultData(w, r)
	projectID := r.URL.Query().Get("id")
	project := project.FindProject(projectID)
	//fmt.Println("check below this line")
	data["youtubeCode"] = project.GetYoutubeID() // causes error,need to find better way
	data["project"] = project
	data["user"] = currentUser
	//data["author"] = user.FindUser(project.UserEmail)
	//fmt.Println(role.Comment)
	display(w, "viewProject", &Page{Title: "Project", Data: data})
}
Beispiel #11
0
func contestMainHandler(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	currentUser := user.FindUser(s.Get("Email"))
	data := setDefaultData(w, r)

	// newContest := role.FindContests()
	// currentContest := role.FindContest(newContest[0].Id.Hex())

	contestId := r.URL.Query().Get("id")
	if contestId == "" {
		newContest := role.FindContests()
		contestId = newContest[0].Id.Hex()
	}
	contest := role.FindContest(contestId)
	data["contest"] = contest
	data["user"] = currentUser
	display(w, "viewContest", &Page{Title: "Contest", Data: data})
}
Beispiel #12
0
func googleCallbackHandler(w http.ResponseWriter, r *http.Request) {
	state := r.FormValue("state")
	if state != oauthStateString {
		fmt.Printf("invalid oauth state, expected '%s', got '%s'\n", oauthStateString, state)
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	code := r.FormValue("code")
	token, err := googleOauthConfig.Exchange(oauth2.NoContext, code)
	if err != nil {
		fmt.Println("Code exchange failed with '%s'\n", err)
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	response, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken)
	defer response.Body.Close()
	contents, err := ioutil.ReadAll(response.Body)

	var gUser GoogleUser
	json.Unmarshal(contents, &gUser)

	currentUser = user.FindUser(gUser.Email)
	if currentUser == nil {
		newUser := user.NewUser(gUser.Email, gUser.Name)
		user.InsertUser(newUser)
		currentUser = newUser
	}

	//get session and set whatever variables you want to access!
	s := redis_session.Session(w, r) // use this to retrieve current user session
	s.Set("DisplayName", currentUser.Email)
	s.Set("Email", currentUser.Email)
	s.Set("ID", currentUser.Id.Hex()) //gives hex value of id for access

	http.Redirect(w, r, "/", http.StatusTemporaryRedirect)

}
Beispiel #13
0
func updateUserPictureHandler(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	currentUser := user.FindUser(s.Get("Email"))

	file, handler, err := r.FormFile("profPic")

	if err == nil {
		defer file.Close()
		bytes, err := file.Seek(0, 2)
		if err != nil {
			panic(err)
		}

		//get file size in kilobytes and megabytes
		var kilobytes int64
		kilobytes = (bytes / 1024)

		var megabytes float64
		megabytes = (float64)(kilobytes / 1024)

		imageID := bson.NewObjectId()

		if megabytes < 4 {
			attachmentURL := "/profiles/" + imageID.Hex() + "/" + s.Get("Email") + "/" + handler.Filename

			uploader := s3manager.NewUploader(session.New())
			result, err := uploader.Upload(&s3manager.UploadInput{
				Body:   file,
				Bucket: aws.String("coaud"),
				Key:    aws.String(attachmentURL),
			})
			if err != nil {
				log.Fatalln("Failed to upload", err)
			}

			log.Println("Successfully uploaded to", result.Location)

			if currentUser.ProfilePictureURL != "/public/img/default_profile_pic.png" {
				svc := s3.New(session.New(), &aws.Config{Region: aws.String("us-east-1")})

				params := &s3.DeleteObjectInput{
					Bucket: aws.String("coaud"),
					Key:    aws.String(currentUser.AwsPictureURL),
				}

				svc.DeleteObject(params)
			}

			log.Println("Successfully deleted from ", attachmentURL)

			user.UpdateUserPicture(s.Get("ID"), result.Location, attachmentURL, currentUser)

			w.Write([]byte("" + result.Location))
		} else {
			w.Write([]byte("rejected"))
		}
	} else {
		w.Write([]byte("Invalid File Type"))
	}

}
Beispiel #14
0
func submitRoleHandler(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	currentUser := user.FindUser(s.Get("Email"))

	fmt.Println(r.FormValue("photo"))
	// converting data to valid format
	rawTraits := strings.Split(r.FormValue("traits"), ",")
	var traits []string
	//clean traits
	for i := 0; i < len(rawTraits); i++ {
		s := strings.TrimSpace(rawTraits[i])
		if s != "" {
			traits = append(traits, s)
		}
	}

	layout := "2006-01-02"
	var UTC *time.Location = time.UTC
	deadline, err := time.ParseInLocation(layout, r.FormValue("deadline"), UTC)
	if err != nil {
		fmt.Println(err)
		return
	}

	age, err := strconv.Atoi(r.FormValue("age"))
	if err != nil {
		fmt.Println(err)
		return
	}

	file, handler, err := r.FormFile("photo")

	if err == nil {
		defer file.Close()
		bytes, err := file.Seek(0, 2)
		if err != nil {
			panic(err)
		}

		//get file size in kilobytes and megabytes
		var kilobytes int64
		kilobytes = (bytes / 1024)

		var megabytes float64
		megabytes = (float64)(kilobytes / 1024)

		// adding new role into db
		roleID := bson.NewObjectId()
		//TODO: add appropriate size limit
		// sss := strconv.FormatFloat(megabytes, 'f', 6, 64)
		// fmt.Println("Size: " + sss )
		if megabytes < 4 {
			attachmentURL := "/roles/" + roleID.Hex() + "/" + s.Get("Email") + "/" + handler.Filename

			uploader := s3manager.NewUploader(session.New())
			result, err := uploader.Upload(&s3manager.UploadInput{
				Body:   file,
				Bucket: aws.String("coaud"),
				Key:    aws.String(attachmentURL),
			})

			if err != nil {
				log.Fatalln("Failed to upload", err)
			}

			log.Println("Successfully uploaded to", result.Location)

			newRole := role.NewRole(r.FormValue("title"), currentUser, r.FormValue("description"), r.FormValue("script"), deadline, traits, age, r.FormValue("gender"), roleID, result.Location)
			role.InsertRole(newRole)

			// urlParts := []string{"/auditions/?id=", newRole.Id.Hex()}
			// url := strings.Join(urlParts, "")
			w.Write([]byte(newRole.Id.Hex()))
			//http.Redirect(w, r, url, http.StatusTemporaryRedirect)
		} else {
			//handle response if greater than 4 megabytes! -- NEED TO MAKE RESPONSIVE
			w.Write([]byte("rejected"))
		}
	} else {
		fmt.Printf("err opening image file: %s", err)
		fmt.Println("Placing default image..")
		roleID := bson.NewObjectId()

		newRole := role.NewRole(r.FormValue("title"), currentUser, r.FormValue("description"), r.FormValue("script"), deadline, traits, age, r.FormValue("gender"), roleID, "/public/img/default_role_pic.png")
		role.InsertRole(newRole)
		fmt.Println(newRole)
		// urlParts := []string{"/auditions/?id=", newRole.Id.Hex()}
		// url := strings.Join(urlParts, "")

		// http.Redirect(w, r, url, http.StatusTemporaryRedirect)
		w.Write([]byte(newRole.Id.Hex()))
	}

}
Beispiel #15
0
func updateRoleHandler(w http.ResponseWriter, r *http.Request) {
	s := redis_session.Session(w, r)
	currentUser := user.FindUser(s.Get("Email"))
	fmt.Println(currentUser)
	roleId := r.URL.Query().Get("id")
	rawTraits := strings.Split(r.FormValue("traits"), ",")
	var traits []string
	for i := 0; i < len(rawTraits); i++ {
		s := strings.TrimSpace(rawTraits[i])
		if s != "" {
			traits = append(traits, s)
		}
	}
	layout := "2006-01-02"
	var UTC *time.Location = time.UTC
	fmt.Println(r.FormValue("deadline"))
	deadline, err := time.ParseInLocation(layout, r.FormValue("deadline"), UTC)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Have we gotten this far")
	age, err := strconv.Atoi(r.FormValue("age"))
	if err != nil {
		fmt.Println(err)
		return
	}
	file, handler, err := r.FormFile("photo")
	if err == nil {
		fmt.Println("success photo is in there")
		defer file.Close()
		bytes, err := file.Seek(0, 2)
		if err != nil {
			panic(err)
		}
		var kilobytes int64
		kilobytes = (bytes / 1024)

		var megabytes float64
		megabytes = (float64)(kilobytes / 1024)

		if megabytes < 4 {
			attachmentURL := "/roles/" + roleId + "/" + s.Get("Email") + "/" + handler.Filename

			uploader := s3manager.NewUploader(session.New())
			result, err := uploader.Upload(&s3manager.UploadInput{
				Body:   file,
				Bucket: aws.String("coaud"),
				Key:    aws.String(attachmentURL),
			})

			if err != nil {
				log.Fatalln("Failed to upload", err)
			}

			log.Println("Successfully uploaded to", result.Location)

			editedRoleWithPhoto := role.ChangedRoleWithPhoto(r.FormValue("title"), r.FormValue("description"), r.FormValue("script"), deadline, traits, age, r.FormValue("gender"), result.Location)

			role.UpdateRoleWithPhoto(roleId, editedRoleWithPhoto)

			w.Write([]byte(roleId))
		} else {
			w.Write([]byte("rejected"))
		}
	} else {
		fmt.Println("No photo is in there")
		editedRoleWithNoPhoto := role.ChangedRoleNoPhoto(r.FormValue("title"), r.FormValue("description"), r.FormValue("script"), deadline, traits, age, r.FormValue("gender"))

		role.UpdateRoleNoPhoto(roleId, editedRoleWithNoPhoto)

		w.Write([]byte(roleId))
	}

}