using Business.Authentication; using Business.Mobile; using Common; using Common.Model; using JsonRx.AuthFilter; using JsonRx.Helper; using log4net; using Newtonsoft.Json; using System; using System.Linq; using System.Web.Http; using System.Web.Http.ModelBinding; using static Common.Model.DynamicReceiverSetup.DynamicReceiver; namespace JsonRx.Api { /// /// [RoutePrefix("api/v1")] public class ReceiverController : ApiController { private readonly IMobileServices _requestServices; private readonly IAuthenticationBusiness _authenticationBusiness; private static readonly ILog Log = LogManager.GetLogger(typeof(ReceiverController)); /// /// public ReceiverController() { } /// /// /// /// public ReceiverController(IMobileServices requestServices, IAuthenticationBusiness authenticationBusiness) { _requestServices = requestServices; _authenticationBusiness = authenticationBusiness; } /// /// /// /// [HttpGet] [TokenAuthentication] [Route("mobile/receiver/{id}")] public IHttpActionResult GetReceiverInfo(string id) { LogicalThreadContext.Properties[LoggerProperty.PROCESSID] = Guid.NewGuid(); LogicalThreadContext.Properties[LoggerProperty.METHODNAME] = "GetReceiverInfo"; Log.Debug("GetReceiverInfo | REQUEST : " + id); var receiver = _requestServices.GetReceiverDetail(id); return Ok(receiver); } /// /// /// /// /// [HttpPost] [TokenAuthentication] [Route("mobile/receiver/save/{userId}")] public IHttpActionResult SaveReceiver(ReceiverModel model, string userId) { LogicalThreadContext.Properties[LoggerProperty.PROCESSID] = Guid.NewGuid(); LogicalThreadContext.Properties[LoggerProperty.CREATEDBY] = userId; LogicalThreadContext.Properties[LoggerProperty.METHODNAME] = "SaveReceiver"; Log.Debug("SaveReceiver | REQUEST : " + JsonConvert.SerializeObject(model)); if (ModelState.IsValid) { var res = _requestServices.SaveReceiver(model, userId); return Ok(res); } return ModelValidationError(ModelState); } /// /// /// /// /// [HttpPost] [TokenAuthentication] [Route("mobile/receiver/remove/{userId}")] public IHttpActionResult RemoveReceiver(string userId, [FromUri] string receiverId) { LogicalThreadContext.Properties[LoggerProperty.PROCESSID] = Guid.NewGuid(); LogicalThreadContext.Properties[LoggerProperty.CREATEDBY] = userId; LogicalThreadContext.Properties[LoggerProperty.METHODNAME] = "RemoveReceiver"; Log.Debug("RemoveReceiver | REQUEST : " + receiverId); JsonRxResponse res = _requestServices.RemoveReceiver(userId, receiverId); if (res.ErrorCode.Equals("0")) { Receivers rs = new Receivers { recipientId = receiverId }; res.Data = rs; } return Ok(res); } /// /// /// /// protected IHttpActionResult ModelValidationError(ModelStateDictionary modelState) { var modelErrors = modelState.Select(x => x.Value.Errors) .Where(y => y.Count > 0) .First()[0].ErrorMessage; JsonRxResponse jsonRx = new JsonRxResponse() { ErrorCode = "1", Msg = string.IsNullOrEmpty(modelErrors) ? "It seems like incorrect Json input(s)." : modelErrors, Data = "" }; return Ok(jsonRx); } } }