Merge branch 'vnext' of https://github.com/pazof/yavsc.git
commit
367b68e093
@ -0,0 +1,165 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using Microsoft.AspNet.Authorization;
|
||||||
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
|
using Yavsc.Models;
|
||||||
|
using Yavsc.Models.Access;
|
||||||
|
|
||||||
|
namespace Yavsc.Controllers
|
||||||
|
{
|
||||||
|
[Produces("application/json")]
|
||||||
|
[Route("api/blacklist"), Authorize]
|
||||||
|
public class BlackListApiController : Controller
|
||||||
|
{
|
||||||
|
private ApplicationDbContext _context;
|
||||||
|
|
||||||
|
public BlackListApiController(ApplicationDbContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET: api/BlackListApi
|
||||||
|
[HttpGet]
|
||||||
|
public IEnumerable<BlackListed> GetBlackListed()
|
||||||
|
{
|
||||||
|
return _context.BlackListed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET: api/BlackListApi/5
|
||||||
|
[HttpGet("{id}", Name = "GetBlackListed")]
|
||||||
|
public IActionResult GetBlackListed([FromRoute] long id)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return HttpBadRequest(ModelState);
|
||||||
|
}
|
||||||
|
BlackListed blackListed = _context.BlackListed.Single(m => m.Id == id);
|
||||||
|
if (blackListed == null)
|
||||||
|
{
|
||||||
|
return HttpNotFound();
|
||||||
|
}
|
||||||
|
if (!CheckPermission(blackListed))
|
||||||
|
return HttpBadRequest();
|
||||||
|
|
||||||
|
return Ok(blackListed);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool CheckPermission(BlackListed blackListed)
|
||||||
|
{
|
||||||
|
var uid = User.GetUserId();
|
||||||
|
if (uid != blackListed.OwnerId)
|
||||||
|
if (!User.IsInRole(Constants.AdminGroupName))
|
||||||
|
if (!User.IsInRole(Constants.FrontOfficeGroupName))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// PUT: api/BlackListApi/5
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
public IActionResult PutBlackListed(long id, [FromBody] BlackListed blackListed)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return HttpBadRequest(ModelState);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id != blackListed.Id)
|
||||||
|
{
|
||||||
|
return HttpBadRequest();
|
||||||
|
}
|
||||||
|
if (!CheckPermission(blackListed))
|
||||||
|
return HttpBadRequest();
|
||||||
|
_context.Entry(blackListed).State = EntityState.Modified;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_context.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (DbUpdateConcurrencyException)
|
||||||
|
{
|
||||||
|
if (!BlackListedExists(id))
|
||||||
|
{
|
||||||
|
return HttpNotFound();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HttpStatusCodeResult(StatusCodes.Status204NoContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST: api/BlackListApi
|
||||||
|
[HttpPost]
|
||||||
|
public IActionResult PostBlackListed([FromBody] BlackListed blackListed)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return HttpBadRequest(ModelState);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!CheckPermission(blackListed))
|
||||||
|
return HttpBadRequest();
|
||||||
|
|
||||||
|
_context.BlackListed.Add(blackListed);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_context.SaveChanges();
|
||||||
|
}
|
||||||
|
catch (DbUpdateException)
|
||||||
|
{
|
||||||
|
if (BlackListedExists(blackListed.Id))
|
||||||
|
{
|
||||||
|
return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return CreatedAtRoute("GetBlackListed", new { id = blackListed.Id }, blackListed);
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE: api/BlackListApi/5
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
public IActionResult DeleteBlackListed(long id)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return HttpBadRequest(ModelState);
|
||||||
|
}
|
||||||
|
|
||||||
|
BlackListed blackListed = _context.BlackListed.Single(m => m.Id == id);
|
||||||
|
if (blackListed == null)
|
||||||
|
{
|
||||||
|
return HttpNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!CheckPermission(blackListed))
|
||||||
|
return HttpBadRequest();
|
||||||
|
|
||||||
|
_context.BlackListed.Remove(blackListed);
|
||||||
|
_context.SaveChanges();
|
||||||
|
|
||||||
|
return Ok(blackListed);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
_context.Dispose();
|
||||||
|
}
|
||||||
|
base.Dispose(disposing);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool BlackListedExists(long id)
|
||||||
|
{
|
||||||
|
return _context.BlackListed.Count(e => e.Id == id) > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
using Yavsc.Models.Billing;
|
||||||
|
|
||||||
|
namespace Yavsc.Helpers
|
||||||
|
{
|
||||||
|
public static class EstimateHelpers {
|
||||||
|
public static decimal GetTotal(this Estimate estimate)
|
||||||
|
{
|
||||||
|
decimal result = 0;
|
||||||
|
foreach (var l in estimate.Bill)
|
||||||
|
result += l.Count*l.UnitaryCost;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using YavscLib;
|
||||||
|
|
||||||
|
namespace Yavsc.Models.Access
|
||||||
|
{
|
||||||
|
public class BlackListed: IBlackListed
|
||||||
|
{
|
||||||
|
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||||
|
public long Id { get; set; }
|
||||||
|
public string UserId { get; set; }
|
||||||
|
public string OwnerId { get; set; }
|
||||||
|
|
||||||
|
[ForeignKey("OwnerId")]
|
||||||
|
public virtual ApplicationUser Owner { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,54 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using Microsoft.Extensions.Localization;
|
||||||
|
using Yavsc.Helpers;
|
||||||
|
using Yavsc.Models.Billing;
|
||||||
|
|
||||||
|
namespace Yavsc.Models.Messaging
|
||||||
|
{
|
||||||
|
public class EstimationEvent: IEvent
|
||||||
|
{
|
||||||
|
public EstimationEvent(ApplicationDbContext context, Estimate estimate, IStringLocalizer SR)
|
||||||
|
{
|
||||||
|
Estimation = estimate;
|
||||||
|
var perfer = context.Performers.FirstOrDefault(
|
||||||
|
p => p.PerformerId == estimate.OwnerId
|
||||||
|
);
|
||||||
|
// Use estimate.OwnerId;
|
||||||
|
ProviderInfo = new ProviderClientInfo {
|
||||||
|
Rate = perfer.Rate,
|
||||||
|
UserName = perfer.Performer.UserName,
|
||||||
|
Avatar = perfer.Performer.Avatar,
|
||||||
|
UserId = perfer.PerformerId
|
||||||
|
};
|
||||||
|
((IEvent)this).Sender = perfer.Performer.UserName;
|
||||||
|
((IEvent)this).Message = string.Format(SR["EstimationMessageToClient"],perfer.Performer.UserName,
|
||||||
|
estimate.Title,estimate.GetTotal());
|
||||||
|
}
|
||||||
|
ProviderClientInfo ProviderInfo { get; set; }
|
||||||
|
Estimate Estimation { get; set; }
|
||||||
|
|
||||||
|
private string subtopic = null;
|
||||||
|
string IEvent.Topic
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return "/topic/estimate"+subtopic!=null?"/"+subtopic:"";
|
||||||
|
}
|
||||||
|
|
||||||
|
set
|
||||||
|
{
|
||||||
|
subtopic = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string IEvent.Sender
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
|
string IEvent.Message
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
namespace Yavsc.Models.Messaging
|
||||||
|
{
|
||||||
|
public class ProviderClientInfo : ClientProviderInfo
|
||||||
|
{
|
||||||
|
public int Rate { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
namespace YavscLib
|
||||||
|
{
|
||||||
|
public interface IBlackListed
|
||||||
|
{
|
||||||
|
long Id { get; set; }
|
||||||
|
string UserId { get; set; }
|
||||||
|
string OwnerId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue