Reputation: 3
Add same member many times to gather with add card trello but don't work
PROBLEM
When I input an same username, for example
{ "adwin", "adwin", "robert", "dave"}
If first adwin finishes loop add member is ok, then second adwin time to add.
The code exit loop and stop working.
and
When I input usernames aren't in the board or username is fake.
{ "hghg$*#hgh", "us7s5209hf9", "8shjd76dhj3" }
When add card finish and come to add Member It's stop working and exit program.
It's not working loop to finish to add another username.
Plan
I think add Member to a board before add into a card will work but it not work again.
#r"Manatee.Trello.dll"
#r"Manatee.Json.dll"
using System.Net;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using Manatee.Trello;
using Manatee.Json;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
string Appkey = "df45ee60b0ff10c19377354490f7054b";
string Token = "6af303020a7a4d24d9cf5a1dd8adf2e5ff95c46888b0371c0b2dba45b2a32d7d";
string BoardID = "5b40363293193091c1ba5013";
string ListID = "5b40363293193091c1ba5015";
string name ="CardAddMember";
string description ="desccccc";
string duedate ="2019-08-04";
string iscomplete =true;
string[] user = {"userinboard","notusername","userinboard"};
try
{
DateTime duedatetime = DateTime.Parse(duedate);//time
bool torf = Boolean.Parse(iscomplete);//finish work check
TrelloAuthorization.Default.AppKey = Appkey;
TrelloAuthorization.Default.UserToken = Token;
ITrelloFactory factory = new TrelloFactory();
var List = factory.List(ListID);
var Board = factory.Board(BoardID);
//var MemberType = factory.Board();
char[] commaSeparator = new char[] { ',' };
string[] authors = username.Split(commaSeparator, StringSplitOptions.None);
var card = await List.Cards.Add(name, description, null, duedatetime, torf, null, null);//add card to trello
foreach (string author in authors){
var Member = factory.Member(author);
await Board.Memberships.Add(Member); // Add member in a board but cant add <<<<<<<<<????????
await card.Members.Add(Member); //add member in card
}
var response = new HttpResponseMessage(HttpStatusCode.OK);
return response;
}
catch //find some err [example not find user]
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
}
What should I do ? Cr.marc_s edit world
Upvotes: 0
Views: 304
Reputation: 8428
You're going to need to check the members before you add them. Trello returns an error when you attempt to add a member to a board/card but that member already exists. This, in turn, will cause Manatee.Trello to throw an exception.
Additionally, a member must belong to a board before they can be assigned to a card.
Checking for these members first should resolve your problem.
await Board.Memberships.Refresh(); // this will fetch all of the members of the board
foreach (string author in authors){
var Member = factory.Member(author);
if (!Board.Memberships.Any(m => m.Member == Member))
await Board.Memberships.Add(Member);
if (!Card.Members.Contains(Member))
await card.Members.Add(Member);
}
This should resolve the problems you're seeing.
Lastly, when you catch an exception, it's helpful to specify the exception and possible print out or log the result. Trello's pretty good at returning error messages with their failure responses, and Manatee.Trello will include them in its TrelloInteractionException
s.
Upvotes: 0