~hokiegeek/ytpulse

578c79488d43b767eee72ee8cf642e93030f36fc — HokieGeek 2 months ago 07b0d8e main 0.3.1-1
Corrects program day item management that I had totally botched
2 files changed, 46 insertions(+), 118 deletions(-)

M program.go
M serve.go
M program.go => program.go +19 -76
@@ 14,44 14,6 @@ type Program struct {
	UserData userData `json:"userData,omitempty"`
}

/*
func (p Program) asYtlCollection() (ytlibrarian.Collection, error) {
	var ytl ytlibrarian.Collection
	ytl.ID = p.ID
	ytl.ReferenceID = p.ReferenceID
	ytl.Description = p.Description
	ytl.Title = p.Title
	ytl.ThumbnailURLs = p.ThumbnailURLs
	ytl.DurationSecs = p.DurationSecs
	ytl.PrivacyStatus = p.PrivacyStatus
	ytl.Source = p.Source
	ytl.URL = p.URL

	deets, err := json.Marshal(&p.UserData)
	ytl.Data = string(deets)
	return ytl, err
}

func ytlCollectionAsProgram(ytlc ytlibrarian.Collection) (Program, error) {
	var p Program
	p.ID = ytlc.ID
	p.ReferenceID = ytlc.ReferenceID
	p.Description = ytlc.Description
	p.Title = ytlc.Title
	p.ThumbnailURLs = ytlc.ThumbnailURLs
	p.DurationSecs = ytlc.DurationSecs
	p.PrivacyStatus = ytlc.PrivacyStatus
	p.Source = ytlc.Source
	p.URL = ytlc.URL

	var err error
	if ytlc.Data != "" {
		err = json.Unmarshal([]byte(ytlc.Data), &p.UserData)
	}
	return p, err
}
*/

func (p Program) asPulseCollection() pulseCollection {
	var pc pulseCollection
	pc.ID = p.ID


@@ 114,44 76,6 @@ type ProgramDay struct {
	UserData userData `json:"userData,omitempty"`
}

/*
func (p ProgramDay) asYtlCollection() (ytlibrarian.Collection, error) {
	var ytl ytlibrarian.Collection
	ytl.ID = p.ID
	ytl.ReferenceID = p.ReferenceID
	ytl.Description = p.Description
	ytl.Title = p.Title
	ytl.ThumbnailURLs = p.ThumbnailURLs
	ytl.DurationSecs = p.DurationSecs
	ytl.PrivacyStatus = p.PrivacyStatus
	ytl.Source = p.Source
	ytl.URL = p.URL

	deets, err := json.Marshal(&p.UserData)
	ytl.Data = string(deets)
	return ytl, err
}

func ytlCollectionAsProgramDay(ytlc ytlibrarian.Collection) (ProgramDay, error) {
	var p ProgramDay
	p.ID = ytlc.ID
	p.ReferenceID = ytlc.ReferenceID
	p.Description = ytlc.Description
	p.Title = ytlc.Title
	p.ThumbnailURLs = ytlc.ThumbnailURLs
	p.DurationSecs = ytlc.DurationSecs
	p.PrivacyStatus = ytlc.PrivacyStatus
	p.Source = ytlc.Source
	p.URL = ytlc.URL

	var err error
	if ytlc.Data != "" {
		err = json.Unmarshal([]byte(ytlc.Data), &p.UserData)
	}
	return p, err
}
*/

func (p ProgramDay) asPulseCollection() pulseCollection {
	var pc pulseCollection
	pc.ID = p.ID


@@ 379,6 303,25 @@ func GetProgramDayByID(librarian *ytlibrarian.Librarian, user User, id uuid.UUID
	return pc.asProgramDay(), nil
}

// GetProgramDayByPos retrieves the program day which matches the given position
func GetProgramDayByPos(librarian *ytlibrarian.Librarian, user User, programID uuid.UUID, pos int) (ProgramDay, error) {
	days, err := GetProgramDays(librarian, programID)
	if err != nil {
		return ProgramDay{}, err
	}
	for _, day := range days.Days {
		if day.Position == pos {
			dayID, err := uuid.Parse(day.ID)
			if err != nil {
				return ProgramDay{}, err
			}
			return GetProgramDayByID(librarian, user, dayID)
		}
	}

	return ProgramDay{}, &ytlibrarian.ThingNotFoundError{Err: fmt.Errorf("did not find a ProgramDay at position %d", pos)}
}

// InsertProgramDay adds a day to a Program
func InsertProgramDay(librarian *ytlibrarian.Librarian, user User, programID, dayID uuid.UUID, dayPos int) error {
	ytluser := user.asYtlUser()

M serve.go => serve.go +27 -42
@@ 1869,13 1869,30 @@ func getProgramDayItems(c *gin.Context) {
	}
	defer librarian.Close()

	requestedID, err := uuid.Parse(c.Param("id"))
	user, _, valid := getAuthentication(c, librarian)
	if !valid {
		return
	}

	programID, err := uuid.Parse(c.Param("id"))
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	items, err := GetProgramDayItems(librarian, requestedID)
	dayPos, err := strconv.Atoi(c.Param("day"))
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	programDay, err := GetProgramDayByPos(librarian, user, programID, dayPos)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	items, err := GetProgramDayItems(librarian, programDay.ID)
	var nterr *ytlibrarian.ThingNotFoundError
	if errors.As(err, &nterr) {
		log.Printf("warning: could not find something....: %v", err)


@@ 2050,21 2067,6 @@ func insertProgramDayItem(c *gin.Context) {
	if err != nil {
		var nterr *ytlibrarian.ThingNotFoundError
		if errors.As(err, &nterr) {
			// c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": err.Error()})
			/*
				programID, err := uuid.Parse(c.Param("id"))
				if err != nil {
					c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
					return
				}

				dayPos, err := strconv.Atoi(c.Param("day"))
				if err != nil {
					c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
					return
				}
			*/

			programDay, err := insertEmptyProgramDay(c, librarian, user)
			if err != nil {
				var nterr *ytlibrarian.ThingNotFoundError


@@ 2073,36 2075,19 @@ func insertProgramDayItem(c *gin.Context) {
				} else {
					c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
				}
				return
			}
			programDayID = programDay.ID

			/*
				programDay, err := CreateProgramDay(librarian, ProgramDay{}, user)
				if err != nil {
					c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
					return
				}

				if err := InsertProgramDay(librarian, user, programID, programDay.ID, dayPos); err != nil {
					var nterr *ytlibrarian.ThingNotFoundError
					if errors.As(err, &nterr) {
						c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": err.Error()})
					} else {
						c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
					}
					// return
				}
			*/
		} else {
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
	} else {
		programDayID, err = uuid.Parse(programDayItem.ID)
		if err != nil {
			c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		return
	}

	programDayID, err = uuid.Parse(programDayItem.ID)
	if err != nil {
		c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	itemPos, err := strconv.Atoi(c.Param("pos"))