Beispiel #1
0
func UpdateEndpoint(c *middleware.Context, cmd m.UpdateEndpointCommand) Response {
	cmd.OrgId = c.OrgId

	err := bus.Dispatch(&cmd)
	if err != nil {
		return ApiError(500, "Failed to update endpoint", err)
	}

	return ApiSuccess("Endpoint updated")
}
Beispiel #2
0
func UpdateEndpoint(cmd *m.UpdateEndpointCommand) error {
	return inTransaction2(func(sess *session) error {
		q := m.GetEndpointByIdQuery{
			Id:    cmd.Id,
			OrgId: cmd.OrgId,
		}
		err := GetEndpointById(&q)
		if err != nil {
			return err
		}
		lastState := q.Result

		endpoint := &m.Endpoint{
			OrgId:   cmd.OrgId,
			Name:    cmd.Name,
			Created: time.Now(),
			Updated: time.Now(),
		}
		endpoint.UpdateEndpointSlug()

		_, err = sess.Id(cmd.Id).Update(endpoint)
		if err != nil {
			return err
		}
		rawSql := "DELETE FROM endpoint_tag WHERE endpoint_id=? and org_id=?"
		if _, err := sess.Exec(rawSql, cmd.Id, cmd.OrgId); err != nil {
			return err
		}
		if len(cmd.Tags) > 0 {
			endpointTags := make([]m.EndpointTag, 0, len(cmd.Tags))
			for _, tag := range cmd.Tags {
				endpointTags = append(endpointTags, m.EndpointTag{
					OrgId:      cmd.OrgId,
					EndpointId: cmd.Id,
					Tag:        tag,
				})
			}
			sess.Table("endpoint_tag")
			if _, err := sess.Insert(&endpointTags); err != nil {
				return err
			}
		}

		cmd.Result = &m.EndpointDTO{
			Id:    cmd.Id,
			OrgId: endpoint.OrgId,
			Name:  endpoint.Name,
			Slug:  endpoint.Slug,
			Tags:  cmd.Tags,
		}
		sess.publishAfterCommit(&events.EndpointUpdated{
			EndpointPayload: events.EndpointPayload{
				Id:    cmd.Id,
				OrgId: endpoint.OrgId,
				Name:  endpoint.Name,
				Slug:  endpoint.Slug,
				Tags:  cmd.Tags,
			},
			Timestamp: endpoint.Updated,
			LastState: &events.EndpointPayload{
				Id:    lastState.Id,
				OrgId: lastState.OrgId,
				Name:  lastState.Name,
				Slug:  lastState.Slug,
				Tags:  lastState.Tags,
			},
		})
		return nil
	})
}