|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/ovh/cds/engine/api/broadcast" |
| 9 | + "github.com/ovh/cds/engine/api/project" |
| 10 | + "github.com/ovh/cds/sdk" |
| 11 | +) |
| 12 | + |
| 13 | +func (api *API) addBroadcastHandler() Handler { |
| 14 | + return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { |
| 15 | + var bc sdk.Broadcast |
| 16 | + if err := UnmarshalBody(r, &bc); err != nil { |
| 17 | + return sdk.WrapError(err, "addBroadcast> cannot unmarshal body") |
| 18 | + } |
| 19 | + |
| 20 | + if bc.Title == "" { |
| 21 | + return sdk.WrapError(sdk.ErrWrongRequest, "updateBroadcast> wrong title") |
| 22 | + } |
| 23 | + now := time.Now() |
| 24 | + bc.Created = now |
| 25 | + bc.Updated = now |
| 26 | + |
| 27 | + if bc.ProjectKey != "" { |
| 28 | + proj, errProj := project.Load(api.mustDB(), api.Cache, bc.ProjectKey, getUser(ctx)) |
| 29 | + if errProj != nil { |
| 30 | + return sdk.WrapError(sdk.ErrNoProject, "addBroadcast> Cannot load %s", bc.ProjectKey) |
| 31 | + } |
| 32 | + bc.ProjectID = &proj.ID |
| 33 | + } |
| 34 | + |
| 35 | + if err := broadcast.Insert(api.mustDB(), &bc); err != nil { |
| 36 | + return sdk.WrapError(err, "addBroadcast> cannot add broadcast") |
| 37 | + } |
| 38 | + |
| 39 | + return WriteJSON(w, bc, http.StatusCreated) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (api *API) updateBroadcastHandler() Handler { |
| 44 | + return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { |
| 45 | + broadcastID, errr := requestVarInt(r, "id") |
| 46 | + if errr != nil { |
| 47 | + return sdk.WrapError(errr, "updateBroadcast> Invalid id") |
| 48 | + } |
| 49 | + |
| 50 | + if _, err := broadcast.LoadByID(api.mustDB(), broadcastID); err != nil { |
| 51 | + return sdk.WrapError(err, "updateBroadcast> cannot load broadcast by id") |
| 52 | + } |
| 53 | + |
| 54 | + // Unmarshal body |
| 55 | + var bc sdk.Broadcast |
| 56 | + if err := UnmarshalBody(r, &bc); err != nil { |
| 57 | + return sdk.WrapError(err, "updateBroadcast> cannot unmarshal body") |
| 58 | + } |
| 59 | + |
| 60 | + if bc.ProjectKey != "" { |
| 61 | + proj, errProj := project.Load(api.mustDB(), api.Cache, bc.ProjectKey, getUser(ctx)) |
| 62 | + if errProj != nil { |
| 63 | + return sdk.WrapError(sdk.ErrNoProject, "updateBroadcast> Cannot load %s", bc.ProjectKey) |
| 64 | + } |
| 65 | + bc.ProjectID = &proj.ID |
| 66 | + } |
| 67 | + |
| 68 | + tx, errtx := api.mustDB().Begin() |
| 69 | + if errtx != nil { |
| 70 | + return sdk.WrapError(errtx, "updateBroadcast> unable to start transaction") |
| 71 | + } |
| 72 | + |
| 73 | + defer tx.Rollback() |
| 74 | + |
| 75 | + if bc.ID <= 0 || broadcastID != bc.ID { |
| 76 | + return sdk.WrapError(sdk.ErrWrongRequest, "requestVarInt> %s is not valid. id in path:%d", bc.ID, broadcastID) |
| 77 | + } |
| 78 | + |
| 79 | + // update broadcast in db |
| 80 | + if err := broadcast.Update(tx, &bc); err != nil { |
| 81 | + return sdk.WrapError(err, "updateBroadcast> cannot update broadcast") |
| 82 | + } |
| 83 | + |
| 84 | + if err := tx.Commit(); err != nil { |
| 85 | + return sdk.WrapError(err, "updateBroadcast> unable to commit transaction") |
| 86 | + } |
| 87 | + |
| 88 | + return WriteJSON(w, bc, http.StatusOK) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func (api *API) deleteBroadcastHandler() Handler { |
| 93 | + return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { |
| 94 | + broadcastID, errr := requestVarInt(r, "id") |
| 95 | + if errr != nil { |
| 96 | + return sdk.WrapError(errr, "deleteBroadcast> Invalid id") |
| 97 | + } |
| 98 | + |
| 99 | + tx, err := api.mustDB().Begin() |
| 100 | + if err != nil { |
| 101 | + return sdk.WrapError(err, "deleteBroadcast> Cannot start transaction") |
| 102 | + } |
| 103 | + |
| 104 | + if err := broadcast.Delete(tx, broadcastID); err != nil { |
| 105 | + return sdk.WrapError(err, "deleteBroadcast: cannot delete broadcast") |
| 106 | + } |
| 107 | + |
| 108 | + if err := tx.Commit(); err != nil { |
| 109 | + return sdk.WrapError(err, "deleteBroadcast> Cannot commit transaction") |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func (api *API) getBroadcastHandler() Handler { |
| 117 | + return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { |
| 118 | + id, errr := requestVarInt(r, "id") |
| 119 | + if errr != nil { |
| 120 | + return sdk.WrapError(errr, "getBroadcast> Invalid id") |
| 121 | + } |
| 122 | + |
| 123 | + broadcast, err := broadcast.LoadByID(api.mustDB(), id) |
| 124 | + if err != nil { |
| 125 | + return sdk.WrapError(err, "getBroadcast> cannot load broadcasts") |
| 126 | + } |
| 127 | + |
| 128 | + return WriteJSON(w, broadcast, http.StatusOK) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func (api *API) getBroadcastsHandler() Handler { |
| 133 | + return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { |
| 134 | + broadcasts, err := broadcast.LoadAll(api.mustDB()) |
| 135 | + if err != nil { |
| 136 | + return sdk.WrapError(err, "getBroadcasts> cannot load broadcasts") |
| 137 | + } |
| 138 | + |
| 139 | + return WriteJSON(w, broadcasts, http.StatusOK) |
| 140 | + } |
| 141 | +} |
0 commit comments