Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Added Simple API Response
Browse files Browse the repository at this point in the history
  • Loading branch information
berviantoleo committed Nov 19, 2018
1 parent 201fa58 commit 9e6df84
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 11 deletions.
42 changes: 42 additions & 0 deletions src/TweetyCore/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Tweety.Models;
using TweetyCore.Utils;

namespace TweetyCore.Controllers
{
[Route("api/v1/tweety")]
[ApiController]
public class ApiController : ControllerBase
{
[HttpPost("find")]
public IActionResult Index([FromBody] Tags requestBody)
{
if (ModelState.IsValid)
{
TwitterConnect twitterConnect = new TwitterConnect();
var result = twitterConnect.ProcessTag(requestBody);

if (result.Count > 0)
{
return Ok(new
{
status = 200,
message = "Success",
result
});
}
else
{
return Ok(new {
status = 200,
message = "Empty Result",
result = new List<string>()
});
};

}
return BadRequest();
}
}
}
6 changes: 3 additions & 3 deletions src/TweetyCore/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public IActionResult Index(Tags tags)
if (ModelState.IsValid)
{
TwitterConnect twitterConnect = new TwitterConnect();
var result = twitterConnect.ProcessTag(tags);
TweetResponse result = twitterConnect.ProcessTag(tags);

if (result.Item1 > 0)
if (result.Count > 0)
{
return View("ShowResult", result.Item2);
return View("ShowResult", result.Data);
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions src/TweetyCore/Models/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class Tags
public string DinasPendidikan{ set; get; }
public string DinasBinamarga{ set; get; }
public string DinasPemuda { set; get; }

[Required(ErrorMessage = "Required")]
public bool isKMP { set; get; }
public bool IsKMP { set; get; }
}
}
14 changes: 14 additions & 0 deletions src/TweetyCore/Models/TweetResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Tweety.Models;

namespace TweetyCore.Models
{
public class TweetResponse
{
public int Count { get; set; }
public TweetResult Data { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/TweetyCore/TweetyCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<UserSecretsId>6beb4f14-be44-4fc5-bea1-822bec2bbbbc</UserSecretsId>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 7 additions & 4 deletions src/TweetyCore/Utils/TwitterConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Tweetinvi.Core.Extensions;
using Tweetinvi.Models;
using Tweety.Models;
using TweetyCore.Models;
using Utils.StringMatching;

namespace TweetyCore.Utils
Expand Down Expand Up @@ -87,10 +88,12 @@ private void Connect()
Auth.ApplicationCredentials = new TwitterCredentials(customer_key, customer_secret, token, token_secret);
}

public Tuple<int, TweetResult> ProcessTag(Tags tags)
public TweetResponse ProcessTag(Tags tags)
{
int sumOfTweets = ParseTag(tags);
return Tuple.Create(sumOfTweets, tweetResults);
return new TweetResponse(){ Count = sumOfTweets,
Data = tweetResults
};
}

private int ParseTag(Tags tag)
Expand Down Expand Up @@ -132,9 +135,9 @@ private int ParseTag(Tags tag)
{
keywords = tag.DinasSosial;
}
if (keywords != "")
if (keywords != null && keywords != "")
{
GetQuery(category, tweets, keywords, tag.isKMP);
GetQuery(category, tweets, keywords, tag.IsKMP);
}
}
for (int j = 0; j < sumOfTweet; j++)
Expand Down
4 changes: 2 additions & 2 deletions src/TweetyCore/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
@Html.Label("Select Method : ") <br />
<p>
<label>
@Html.RadioButtonFor(model => model.isKMP, true)
@Html.RadioButtonFor(model => model.IsKMP, true)
<span>KMP</span>
</label>
</p>
<p>
<label>
@Html.RadioButtonFor(model => model.isKMP, false)
@Html.RadioButtonFor(model => model.IsKMP, false)
<span>Booyer-Moore</span>
</label>
</p>
Expand Down

0 comments on commit 9e6df84

Please sign in to comment.