photoprism/internal/api/subjects.go

161 lines
3.5 KiB
Go
Raw Normal View History

2021-09-02 16:12:31 +02:00
package api
import (
"net/http"
"github.com/gin-gonic/gin"
2021-09-02 16:12:31 +02:00
"github.com/photoprism/photoprism/internal/acl"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/event"
2021-09-02 16:12:31 +02:00
"github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/internal/i18n"
"github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/pkg/clean"
2021-09-02 16:12:31 +02:00
"github.com/photoprism/photoprism/pkg/txt"
)
// GetSubject returns a subject as JSON.
//
// GET /api/v1/subjects/:uid
func GetSubject(router *gin.RouterGroup) {
router.GET("/subjects/:uid", func(c *gin.Context) {
s := Auth(c, acl.ResourcePeople, acl.ActionView)
2021-09-02 16:12:31 +02:00
if s.Abort(c) {
2021-09-02 16:12:31 +02:00
return
}
if subj := entity.FindSubject(clean.UID(c.Param("uid"))); subj == nil {
2021-09-02 16:12:31 +02:00
Abort(c, http.StatusNotFound, i18n.ErrSubjectNotFound)
return
} else {
c.JSON(http.StatusOK, subj)
}
})
}
// UpdateSubject updates subject properties.
//
// PUT /api/v1/subjects/:uid
func UpdateSubject(router *gin.RouterGroup) {
router.PUT("/subjects/:uid", func(c *gin.Context) {
if err := mutex.UpdatePeople.Start(); err != nil {
AbortBusy(c)
return
}
defer mutex.UpdatePeople.Stop()
s := Auth(c, acl.ResourcePeople, acl.ActionUpdate)
if s.Abort(c) {
return
}
uid := clean.UID(c.Param("uid"))
m := entity.FindSubject(uid)
if m == nil {
Abort(c, http.StatusNotFound, i18n.ErrSubjectNotFound)
return
}
// Initialize form.
f, err := form.NewSubject(*m)
if err != nil {
log.Errorf("subject: %s (new form)", err)
AbortSaveFailed(c)
return
} else if err := c.BindJSON(&f); err != nil {
log.Errorf("subject: %s (update form)", err)
AbortBadRequest(c)
return
}
// Update subject from form values.
if changed, err := m.SaveForm(f); err != nil {
log.Errorf("subject: %s", err)
AbortSaveFailed(c)
return
} else if changed {
if m.IsPerson() {
event.SuccessMsg(i18n.MsgPersonSaved)
} else {
event.SuccessMsg(i18n.MsgSubjectSaved)
}
}
c.JSON(http.StatusOK, m)
})
}
// LikeSubject flags a subject as favorite.
//
// POST /api/v1/subjects/:uid/like
//
// Parameters:
//
// uid: string Subject UID
func LikeSubject(router *gin.RouterGroup) {
router.POST("/subjects/:uid/like", func(c *gin.Context) {
s := Auth(c, acl.ResourcePeople, acl.ActionUpdate)
if s.Abort(c) {
return
}
uid := clean.UID(c.Param("uid"))
subj := entity.FindSubject(uid)
if subj == nil {
Abort(c, http.StatusNotFound, i18n.ErrSubjectNotFound)
return
}
if err := subj.Update("SubjFavorite", true); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
return
}
PublishSubjectEvent(EntityUpdated, uid, c)
c.JSON(http.StatusOK, http.Response{})
})
}
// DislikeSubject removes the favorite flag from a subject.
//
// DELETE /api/v1/subjects/:uid/like
//
// Parameters:
//
// uid: string Subject UID
func DislikeSubject(router *gin.RouterGroup) {
router.DELETE("/subjects/:uid/like", func(c *gin.Context) {
s := Auth(c, acl.ResourcePeople, acl.ActionUpdate)
if s.Abort(c) {
return
}
uid := clean.UID(c.Param("uid"))
subj := entity.FindSubject(uid)
if subj == nil {
Abort(c, http.StatusNotFound, i18n.ErrSubjectNotFound)
return
}
if err := subj.Update("SubjFavorite", false); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UpperFirst(err.Error())})
return
}
PublishSubjectEvent(EntityUpdated, uid, c)
2021-09-02 16:12:31 +02:00
c.JSON(http.StatusOK, http.Response{})
2021-09-02 16:12:31 +02:00
})
}