Skip to content

Commit

Permalink
Merge pull request #8 from hub-burgan-com-tr/test
Browse files Browse the repository at this point in the history
Test
  • Loading branch information
yarasaa authored Mar 13, 2024
2 parents 304d28b + 2e5adc4 commit a8d78cb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 61 deletions.
51 changes: 20 additions & 31 deletions Business/Concrete/VoteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class VoteManager : IVoteService
{
IVoteDal _voteDal;
IUserInfoDal _userInfoDal;
static readonly object obj = new object();

public VoteManager(IVoteDal voteDal, IUserInfoDal userInfoDal)
{
Expand All @@ -24,39 +25,27 @@ public VoteManager(IVoteDal voteDal, IUserInfoDal userInfoDal)

public IResult Add(Vote vote)
{
vote.VoteDate = DateTime.Now.Date;
vote.Date = DateTime.Now;
var result = _userInfoDal.Get(x => x.UserId == vote.UserId);
// if (result.VoteLimit > 0 && result.VoteDate == vote.VoteDate)
// {
// //vote.UserId = null;
// _voteDal.Add(vote);
// result.VoteLimit--;
// result.VoteDate = vote.VoteDate;
// _userInfoDal.Update(result);
// return new SuccessResult(Messages.VoteSuccess);
// }
// else if (result.VoteDate != vote.VoteDate)
// {
// result.VoteDate = vote.VoteDate;
// result.VoteLimit = 0;
// //vote.UserId=null;
// _voteDal.Add(vote);
// _userInfoDal.Update(result);
// return new SuccessResult(Messages.VoteSuccess);
// }
if (result.VoteDate == null || result.VoteDate.Date != vote.VoteDate.Date)
lock (obj)
{
result.VoteDate = vote.VoteDate;
result.VoteLimit = 0;
_voteDal.Add(vote);
_userInfoDal.Update(result);
return new SuccessResult(Messages.VoteSuccess);
}
else
{
return new SuccessResult(Messages.VoteFailed);
vote.VoteDate = DateTime.Now.Date;
vote.Date = DateTime.Now;
var result = _userInfoDal.Get(x => x.UserId == vote.UserId);

if (result.VoteDate == null || result.VoteDate.Date != vote.VoteDate.Date)
{
result.VoteDate = vote.VoteDate;
result.VoteLimit = 0;
_voteDal.Add(vote);
_userInfoDal.Update(result);

return new SuccessResult(Messages.VoteSuccess);
}
else
{
return new SuccessResult(Messages.VoteFailed);
}
}

}

}
Expand Down
31 changes: 12 additions & 19 deletions WebAPI/Controllers/VoteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,30 @@ namespace WebAPI.Controllers
public class VoteController : ControllerBase
{
private readonly IVoteService _voteService;
private readonly IDistributedLockFactory _distributedLockFactory;

public VoteController(IVoteService voteService, IDistributedLockFactory distributedLockFactory)
public VoteController(IVoteService voteService)
{
_voteService = voteService;
_distributedLockFactory = distributedLockFactory;

}

[HttpPost]
public IActionResult Post(Vote vote)
{
string lockKey = $"VoteLock:{vote.UserId}";
using (var redisLock = _distributedLockFactory.CreateLock(lockKey, TimeSpan.FromSeconds(10)))
{
if (redisLock.IsAcquired)
{
var result = _voteService.Add(vote);
if (result.Success)
{
return Ok(result);
}
return BadRequest(result.Message);
}
else

var result = _voteService.Add(vote);
if (result.Success)
{
return BadRequest("Cannot acquire lock. Please try again later.");
return Ok(result);
}
}
return BadRequest(result.Message);



}


}


}

12 changes: 1 addition & 11 deletions WebAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using StackExchange.Redis;
using RedLockNet.SERedis.Configuration;
using WebAPI;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -42,16 +43,6 @@
builder.Services.AddDbContext<UserContext>();
builder.Services.AddDbContext<UserDataContext>();

//builder.Services.AddHostedService<AddDataAutomatically>();


var connectionMultiplexer = ConnectionMultiplexer.Connect(builder.Configuration["Redis"]);
Console.WriteLine(builder.Configuration["Redis"]);
var redLockFactory = RedLockFactory.Create(new List<RedLockEndPoint>
{
new DnsEndPoint(builder.Configuration["Redis"], 6379)
});
builder.Services.AddSingleton<IDistributedLockFactory>(redLockFactory);
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.Enrich.FromLogContext()
Expand Down Expand Up @@ -87,7 +78,6 @@
app.UseSwaggerUI();
}


//app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
Expand Down

0 comments on commit a8d78cb

Please sign in to comment.