// Create adds a Activity to the DB. This function is mapped to the
// path POST /activities
Create adds a Activity to the DB. This function is mapped to the
path POST /activities
// Create adds a Activity to the DB . This function is mapped to the // path POST / activities
func (v ActivitiesResource) Create(c buffalo.Context) error {
	activity := &models.Activity{}
	if err := c.Bind(activity); err != nil {
		return err
	}
	activity.UserID = c.Session().Get("current_user_id").(uuid.UUID)
	tx, ok := c.Value("tx").(*pop.Connection)
	if !ok {
		return fmt.Errorf("no transaction found")
	}
	verrs, err := tx.ValidateAndCreate(activity)
	if err != nil {
		return err
	}
	if verrs.HasAny() {
		return responder.Wants("html", func(c buffalo.Context) error {
			c.Set("errors", verrs)
			c.Set("activity", activity)
			return c.Render(http.StatusUnprocessableEntity, r.HTML("/activities/new.plush.html"))
		}).Wants("json", func(c buffalo.Context) error {
			return c.Render(http.StatusUnprocessableEntity, r.JSON(verrs))
		}).Wants("xml", func(c buffalo.Context) error {
			return c.Render(http.StatusUnprocessableEntity, r.XML(verrs))
		}).Respond(c)
	}
	return responder.Wants("html", func(c buffalo.Context) error {
		c.Flash().Add("success", T.Translate(c, "activity.created.success"))
		return c.Redirect(http.StatusSeeOther, "/activities/%v", activity.ID)
	}).Wants("json", func(c buffalo.Context) error {
		return c.Render(http.StatusCreated, r.JSON(activity))
	}).Wants("xml", func(c buffalo.Context) error {
		return c.Render(http.StatusCreated, r.XML(activity))
	}).Respond(c)
}
func ( v ActivitiesResource ) Create ( c buffalo . Context ) error { activity := & models . Activity { } if err := c . Bind ( activity ) ; err != nil { return err } activity . UserID = c . Session ( ) . Get ( " " ) . ( uuid . UUID ) tx , ok := c . Value ( " " ) . ( * pop . Connection ) if ! ok { return fmt . Errorf ( " " ) } verrs , err := tx . ValidateAndCreate ( activity ) if err != nil { return err } if verrs . HasAny ( ) { return responder . Wants ( " " , func ( c buffalo . Context ) error { c . Set ( " " , verrs ) c . Set ( " " , activity ) return c . Render ( http . StatusUnprocessableEntity , r . HTML ( " " ) ) } ) . Wants ( " " , func ( c buffalo . Context ) error { return c . Render ( http . StatusUnprocessableEntity , r . JSON ( verrs ) ) } ) . Wants ( " " , func ( c buffalo . Context ) error { return c . Render ( http . StatusUnprocessableEntity , r . XML ( verrs ) ) } ) . Respond ( c ) } return responder . Wants ( " " , func ( c buffalo . Context ) error { c . Flash ( ) . Add ( " " , T . Translate ( c , " " ) ) return c . Redirect ( http . StatusSeeOther , " " , activity . ID ) } ) . Wants ( " " , func ( c buffalo . Context ) error { return c . Render ( http . StatusCreated , r . JSON ( activity ) ) } ) . Wants ( " " , func ( c buffalo . Context ) error { return c . Render ( http . StatusCreated , r . XML ( activity ) ) } ) . Respond ( c ) }
Create adds a Activity to the DB.
Create adds a Activity to the DB .
// Allocate an empty Activity // Bind activity to the html form elements // Get the DB connection from the context // Validate the data from the html form // Make the errors available inside the html template // Render again the new.html template that the user can // correct the input. // If there are no errors set a success message // and redirect to the show page