Esempio n. 1
0
func BandVerify(r http.ResponseWriter, rq *http.Request) {
	name := rq.FormValue("name")
	locType := rq.FormValue("loctype")
	var locationId bson.ObjectId
	errorString := "no errors"
	switch locType {
	case "existing":
		if rq.FormValue("location_id") == "" {
			errorString = "No location was selected"
		} else {
			locationId = gomgoweb2_src.ToObjectId(rq.FormValue("location_id"))
		}
		break
	case "new":
		if rq.FormValue("country") != "" {
			locationId = gomgoweb2_src.GenerateId()
			location := gomgoweb2_src.Location{
				City:    rq.FormValue("city"),
				State:   rq.FormValue("state"),
				Country: rq.FormValue("country"),
			}
			doc := gomgoweb2_src.MyDoc{Id: locationId, Value: bson.M{"City": location.City, "State": location.State, "Country": location.Country}}
			err := gomgoweb2_src.AddDoc(doc, gomgoweb2_src.LOCATION_COL)
			if err != nil {
				errorString = "error on location add: " + err.Error()
			}
		} else {
			errorString = "Country is required"
		}
		break
	}
	if errorString == "no errors" {
		id := gomgoweb2_src.GenerateId()
		band := gomgoweb2_src.Band{Name: name, LocationId: locationId, Albums: []gomgoweb2_src.Album{}}
		doc := gomgoweb2_src.MyDoc{Id: id, Value: bson.M{"Name": band.Name, "LocationId": band.LocationId, "Albums": band.Albums}}
		err := gomgoweb2_src.AddDoc(doc, gomgoweb2_src.BAND_COL)
		if err != nil {
			errorString = "error on band add: " + err.Error()
		}
		t, err := template.ParseFiles("src/gomgoweb2_src/views/band/verify.html")
		if err != nil {
			panic(err)
		} else {
			t.Execute(r, errorString)
		}
	}
}
Esempio n. 2
0
func AlbumVerify(r http.ResponseWriter, rq *http.Request) {
	// implement
	rawId := gomgoweb2_src.ToObjectId(rq.URL.Query()["id"][0])
	id := rawId
	name := rq.FormValue("name")
	yearString := rq.FormValue("year")
	year, _ := strconv.Atoi(yearString)
	genreType := rq.FormValue("genretype")
	var genreId bson.ObjectId
	errorString := "no errors"
	switch genreType {
	case "existing":
		if rq.FormValue("genre_id") == "" {
			errorString = "No genre was selected"
		} else {
			genreId = gomgoweb2_src.ToObjectId(rq.FormValue("genre_id"))
		}
		break
	case "new":
		genre_name := strings.ToUpper(rq.FormValue("genre_name"))
		if rq.FormValue("genre_name") == "" {
			errorString = "Genre name is required to create a genre"
		} else {
			database, session := gomgoweb2_src.GetDB()
			col := database.C(gomgoweb2_src.GENRE_COL)
			query := col.Find(bson.M{"Values.Name": genre_name})
			if q, _ := query.Count(); q == 0 {
				session.Close()
				genreId = gomgoweb2_src.GenerateId()
				genre := gomgoweb2_src.Genre{Name: genre_name}
				doc := gomgoweb2_src.MyDoc{Id: genreId, Value: bson.M{"Name": genre.Name}}
				err := gomgoweb2_src.AddDoc(doc, gomgoweb2_src.GENRE_COL)
				if err != nil {
					errorString = "Genre add error: " + err.Error()
				}
			} else {
				var genres []gomgoweb2_src.MyDoc
				query.All(&genres)
				genreId = genres[0].Id
				session.Close()
			}
		}
		break
	}

	if errorString == "no errors" {
		bandDoc := gomgoweb2_src.GetDoc(rawId, gomgoweb2_src.BAND_COL)
		album := gomgoweb2_src.Album{Name: name, Year: year, GenreId: genreId}
		err := bandDoc.AddAlbum(album)
		if err != nil {
			errorString = "Album add error: " + err.Error()
		}
	}
	t, err := template.ParseFiles("src/gomgoweb2_src/views/album/verify.html")
	if err != nil {
		panic(err)
	} else {
		t.Execute(r, struct {
			Id      string
			Message string
		}{Id: id.String(), Message: errorString})
	}
}