@@ -4,14 +4,14 @@ import (
44 "bytes"
55 "encoding/json"
66 "fmt"
7- "io"
87 "net/http"
8+ "os"
99
1010 "github.com/charmbracelet/huh"
1111 "github.com/spf13/cobra"
1212
13+ "github.com/acmcsufoss/api.acmcsuf.com/internal/cli/client"
1314 "github.com/acmcsufoss/api.acmcsuf.com/internal/cli/config"
14- "github.com/acmcsufoss/api.acmcsuf.com/internal/cli/oauth"
1515 "github.com/acmcsufoss/api.acmcsuf.com/internal/dto"
1616 "github.com/acmcsufoss/api.acmcsuf.com/utils"
1717)
@@ -32,81 +32,56 @@ func init() {
3232}
3333
3434func putAnnouncements (id string , cfg * config.Config ) {
35- resourceURL := config .GetBaseURL (cfg ).JoinPath ("v1" , "announcements" , id )
35+ resourceUrl := config .GetBaseURL (cfg ).JoinPath ("v1" , "announcements" , id )
3636
37- // ----- Get the Announcement We Want to Update -----
38- client := http.Client {}
39- getReq , err := oauth .NewRequestWithAuth (http .MethodGet , resourceURL .String (), nil )
40- if err != nil {
41- fmt .Printf ("Error: couldn't retrieve resource %s: %s" , id , err )
42- return
43- }
44- getRes , err := client .Do (getReq )
45- if err != nil {
46- fmt .Println ("Error: failed to send request:" , err )
47- return
48- }
49- defer getRes .Body .Close ()
50- if getRes .StatusCode != http .StatusOK {
51- fmt .Println ("Error: HTTP" , getRes .Status )
52- return
53- }
54- body , err := io .ReadAll (getRes .Body )
55- if err != nil {
56- fmt .Println ("Error: failed to read response body:" , err )
37+ // ----- Get announcement we want to update -----
38+ var oldPayload dto.Announcement
39+ if body , err := client .SendRequestAndReadResponse (resourceUrl , false , http .MethodGet , nil ); err != nil {
40+ fmt .Fprintln (os .Stderr , "Error:" , err )
41+ if body != nil {
42+ utils .PrettyPrintJSON (body )
43+ }
5744 return
45+ } else {
46+ err = json .Unmarshal (body , & oldPayload )
47+ if err != nil {
48+ fmt .Fprintln (os .Stderr , "Error: failed to unmarshal response body:" , err )
49+ return
50+ }
5851 }
5952
60- // ----- Update found announceement -----
61- var oldPayload dto.UpdateAnnouncement
62- err = json .Unmarshal (body , & oldPayload )
53+ // ----- Update found announcement -----
54+ newPayload , err := putForm (& oldPayload )
6355 if err != nil {
64- fmt .Println ( "Error: failed to unmarshal response body :" , err )
56+ fmt .Fprintln ( os . Stderr , "Error:" , err )
6557 return
6658 }
67- newPayload , err := putForm ( id )
59+ b , err := json . Marshal ( newPayload )
6860 if err != nil {
69- fmt .Println ( "Error:" , err )
61+ fmt .Fprintln ( os . Stderr , "Error: failed to marshal data :" , err )
7062 return
7163 }
72- jsonPayload , err := json .Marshal (newPayload )
73- if err != nil {
74- fmt .Println ("Error: failed to marshal data:" , err )
75- return
76- }
77- putRequest , err := oauth .NewRequestWithAuth (http .MethodPut , resourceURL .String (), bytes .NewBuffer (jsonPayload ))
78- if err != nil {
79- fmt .Println ("Error: failed to contruct request:" , err )
80- return
81- }
82- putResponse , err := client .Do (putRequest )
83- if err != nil {
84- fmt .Println ("Error: failed to send request: " , err )
85- return
86- }
87- defer putResponse .Body .Close ()
8864
89- if putResponse .StatusCode != http .StatusOK {
90- fmt .Println ("Error: HTTP" , putResponse .Status )
91- return
92- }
93- body , err = io .ReadAll (putResponse .Body )
94- if err != nil {
95- fmt .Println ("Error: failed to read response body" , err )
96- return
65+ // Update remote resource with new data
66+ if body , err := client .SendRequestAndReadResponse (resourceUrl , true , http .MethodPut ,
67+ bytes .NewBuffer (b )); err != nil {
68+ fmt .Fprintln (os .Stderr , "Error:" , err )
69+ if body != nil {
70+ utils .PrettyPrintJSONErr (body )
71+ }
72+ } else {
73+ utils .PrettyPrintJSON (body )
9774 }
98- utils .PrettyPrintJSON (body )
9975}
10076
101- // TODO: Use DTO models instaad of dbmodels
102- func putForm (uuid string ) (* dto.UpdateAnnouncement , error ) {
77+ func putForm (oldPayload * dto.Announcement ) (* dto.UpdateAnnouncement , error ) {
10378 var payload dto.UpdateAnnouncement
10479 var err error
10580 var (
106- visibilityStr string
107- announceAtStr string
108- channelIDStr string
109- messageIDStr string
81+ visibilityStr string = oldPayload . Visibility
82+ announceAtStr string // no default for now bc its stored as a raw timestamp
83+ channelIDStr string = * oldPayload . DiscordChannelID
84+ messageIDStr string = * oldPayload . DiscordMessageID
11085 )
11186 form := huh .NewForm (
11287 huh .NewGroup (
@@ -131,8 +106,7 @@ func putForm(uuid string) (*dto.UpdateAnnouncement, error) {
131106 return nil , err
132107 }
133108
134- payload .Uuid = uuid
135- // HACK: These conversions won't be necessary once we start using DTO models here
109+ payload .Uuid = oldPayload .Uuid
136110 payload .Visibility = & visibilityStr
137111 if announceAtStr != "" {
138112 timestamp , err := utils .ByteSlicetoUnix ([]byte (announceAtStr ))
@@ -141,7 +115,6 @@ func putForm(uuid string) (*dto.UpdateAnnouncement, error) {
141115 }
142116 payload .AnnounceAt = & timestamp
143117 }
144-
145118 payload .DiscordChannelID = & channelIDStr
146119 payload .DiscordMessageID = & messageIDStr
147120
0 commit comments