public HttpResponseMessage Register([FromBody]RegistrationAccountModel model)
        {
            return SafeAction(() =>
            {
                if (!model.Password.Equals(model.Confirm, StringComparison.InvariantCulture))
                {
                    throw new InvalidPasswordsException();
                }
                if (!CaptchaProvider.Validate(model.Captcha.Id, model.Captcha.Text))
                {
                    throw new InvalidCaptchaCodeException();
                }

                IDelightServices service = new DelightServices();

                if (String.IsNullOrEmpty(model.Type))
                {
                    service.RegisterUser(model.Username, model.Password, model.Email);
                }
                else
                {
                    service.RegisterOwner(model.Username, model.Password, model.Email);
                }
                return Request.CreateResponse(HttpStatusCode.OK, true);
            }, model);
        }
 public HttpResponseMessage AddUserToGroup([FromBody]GroupDTO model)
 {
     return SafeAction(() =>
        {
        IDelightServices service = new DelightServices();
        service.AddUserToGroup(model.GroupId, model.EntityId);
        var response = Request.CreateResponse<bool>(HttpStatusCode.OK, true);
        return response;
        }, model);
 }
 public HttpResponseMessage DeleteVote([FromBody]IdentityModel model)
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         service.DeleteVote(model.Id);
         var response = Request.CreateResponse(HttpStatusCode.OK);
         return response;
     }, model);
 }
 public HttpResponseMessage DeleteUser([FromBody]IdentityModel user)
 {
     return SafeAction(() =>
       {
       IDelightServices service = new DelightServices();
       service.DeleteUser(user.Id);
       var response = Request.CreateResponse<bool>(HttpStatusCode.OK, true);
       return response;
       }, user);
 }
 public HttpResponseMessage GetAvailablePoints()
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         int availablePoints = service.GetAvailablePoints(User.Identity.Name);
         var response = Request.CreateResponse<int>(HttpStatusCode.OK, availablePoints);
         return response;
     });
 }
 public HttpResponseMessage GetAllVotedPoints()
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         int allPoints = service.GetAllVotedPoints();
         var response = Request.CreateResponse<int>(HttpStatusCode.OK, allPoints);
         return response;
     });
 }
        public HttpResponseMessage Authenticate([FromBody]AuthenticationAccountModel model)
        {
            return SafeAction(() =>
            {
                IDelightServices service = new DelightServices();
                var id = service.Authenticate(model.Username, model.Password, model.Remember);
                CacheCookieProvider.CreateCookie(id).SetCookie();
                FormsAuthentication.SetAuthCookie(model.Username, model.Remember);

                return Request.CreateResponse(HttpStatusCode.OK, GetUserStateInfo(true, model.Username, id));
            }, model);
        }
 public HttpResponseMessage GetProductsFromCategory([FromBody]IdentityModel model)
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         var products = service.GetProductsFromCategory(User.Identity.Name, model.Id);
         if (products == null)
         {
             return Request.CreateResponse(HttpStatusCode.NoContent);
         }
         var response = Request.CreateResponse<IEnumerable<VotedProductDTO>>(HttpStatusCode.OK, products);
         return response;
     }, model);
 }
 public HttpResponseMessage GetCategory([FromBody]IdentityModel model)
 {
     return SafeAction(() =>
       {
       IDelightServices service = new DelightServices();
       var category = service.GetCategory(model.Id);
       if (category == null)
       {
           return Request.CreateResponse(HttpStatusCode.NoContent);
       }
       var response = Request.CreateResponse<CategoryDTO>(HttpStatusCode.OK, category);
       return response;
       }, model);
 }
 public HttpResponseMessage GetVotes()
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         var votes = service.GetVotes();
         if (votes == null)
         {
             return Request.CreateResponse(HttpStatusCode.NoContent);
         }
         var response = Request.CreateResponse<IEnumerable<VotedProductDTO>>(HttpStatusCode.OK, votes);
         return response;
     });
 }
 public HttpResponseMessage GetUser([FromBody]IdentityModel model)
 {
     return SafeAction(() =>
       {
       IDelightServices service = new DelightServices();
       var user = service.GetUser(model.Id);
       if (user == null)
       {
           return Request.CreateResponse(HttpStatusCode.NoContent);
       }
       var response = Request.CreateResponse<UserDTO>(HttpStatusCode.OK, user);
       return response;
       }, model);
 }
 public HttpResponseMessage SaveProduct([FromBody]PricedProductDTO product)
 {
     return SafeAction(() =>
        {
        IDelightServices service = new DelightServices();
        int id = service.SaveProduct(product);
        if (id <= 0)
        {
            return Request.CreateResponse(HttpStatusCode.NoContent);
        }
        var response = Request.CreateResponse<int>(HttpStatusCode.OK, id);
        return response;
        }, product);
 }
 public HttpResponseMessage SaveUser(UserDTO user)
 {
     return SafeAction(() =>
       {
       IDelightServices service = new DelightServices();
       int id = service.SaveUser(user);
       if (id <= 0)
       {
           return Request.CreateResponse(HttpStatusCode.NoContent);
       }
       var response = Request.CreateResponse<int>(HttpStatusCode.OK, id);
       return response;
       }, user);
 }
 public HttpResponseMessage GetUsers()
 {
     return SafeAction(() =>
       {
       IDelightServices service = new DelightServices();
       var users = service.GetUsers();
       if (users == null)
       {
           return Request.CreateResponse(HttpStatusCode.NoContent);
       }
       var response = Request.CreateResponse<IEnumerable<UserDTO>>(HttpStatusCode.OK, users);
       return response;
       });
 }
 public HttpResponseMessage GetAllUsersCompareToSpecificGroup([FromBody]IdentityModel model)
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         var user = service.GetAllUsersCompareToSpecificGroup(model.Id)
             .OrderByDescending(x => x.InGroup);
         if (user == null)
         {
             return Request.CreateResponse(HttpStatusCode.NoContent);
         }
         var response = Request.CreateResponse<IEnumerable<UserFromGroupDTO>>(HttpStatusCode.OK, user);
         return response;
     }, model);
 }
 public HttpResponseMessage GetAllProductsCompareToSpecificCategory([FromBody]IdentityModel model)
 {
     return SafeAction(() =>
        {
        IDelightServices service = new DelightServices();
        var products = service.GetAllProductsCompareToSpecificCategory(model.Id)
            .OrderByDescending(x => x.InGroup); ;
        if (products == null)
        {
            return Request.CreateResponse(HttpStatusCode.NoContent);
        }
        var response = Request.CreateResponse<IEnumerable<ProductFromCategoryDTO>>(HttpStatusCode.OK, products);
        return response;
        }, model);
 }
 public HttpResponseMessage SaveCategory([FromBody]CategoryDTO category)
 {
     return SafeAction(() =>
       {
       IDelightServices service = new DelightServices();
       int id = service.SaveCategory(category);
       if (id <= 0)
       {
           return Request.CreateResponse(HttpStatusCode.NoContent);
       }
       var response = Request.CreateResponse<int>(HttpStatusCode.OK, id);
       return response;
       }, category);
 }
        public HttpResponseMessage GetBudgetReport([FromBody]BugetModel model)
        {
            return SafeAction(() =>
            {
                IDelightServices service = new DelightServices();
                var budget = service.GetBudget(model.Money);

                var report = new BudgetReportChart();
                report.Generate(budget.ToArray(), model.Money);

                var file = DateTime.Now.Ticks.ToString();
                var path = Path.Combine(HostingEnvironment.MapPath("~/Content/Files"), file) + ".xls";
                GenerateReport(path, report);

                var response = Request.CreateResponse<ChartModel>(HttpStatusCode.OK, new ChartModel()
                {
                    Report = report,
                    Url = Path.Combine("/Content/Files", file) + ".xls"
                });
                return response;
            }, model);
        }
 public HttpResponseMessage StartSession()
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         service.StartSession();
         var response = Request.CreateResponse<Boolean>(HttpStatusCode.OK, true);
         return response;
     });
 }
 public HttpResponseMessage SaveWelcomeMessage(MessageDTO message)
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         service.SaveMessage(message);
         var response = Request.CreateResponse<Boolean>(HttpStatusCode.OK, true);
         return response;
     });
 }
 public HttpResponseMessage GetWelcomeMessage()
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         var message = service.GetMessage();
         if (message == null)
         {
             return Request.CreateResponse(HttpStatusCode.NoContent);
         }
         var response = Request.CreateResponse<MessageDTO>(HttpStatusCode.OK, message);
         return response;
     });
 }
        public HttpResponseMessage GetProductVoteReport()
        {
            return SafeAction(() =>
            {
                IDelightServices service = new DelightServices();
                var report = service.GetProductVoteReport();
                if (report == null)
                {
                    return Request.CreateResponse(HttpStatusCode.NoContent);
                }

                var file = DateTime.Now.Ticks.ToString();
                var path = Path.Combine(HostingEnvironment.MapPath("~/Content/Files"), file) + ".xls";
                GenerateReport(path, report);

                var response = Request.CreateResponse<ChartModel>(HttpStatusCode.OK, new ChartModel()
                {
                    Report = report,
                    Url = Path.Combine("/Content/Files", file) + ".xls"
                });
                return response;
            });
        }
 public HttpResponseMessage GetVotedCategories()
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         var categories = service.GetVotedCategories(User.Identity.Name);
         if (categories == null)
         {
             return Request.CreateResponse(HttpStatusCode.NoContent);
         }
         var response = Request.CreateResponse<IEnumerable<VotedCategoryDTO>>(HttpStatusCode.OK, categories);
         return response;
     });
 }
 public HttpResponseMessage RemoveUserFromGroup([FromBody]IdentityDTO model)
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         service.RemoveUserFromGroup(model.Id);
         var response = Request.CreateResponse<bool>(HttpStatusCode.OK, true);
         return response;
     }, model);
 }
        private UserModel GetUserStateInfo(Boolean isAuthenticated, String username, Int32 id = 0)
        {
            var model = new UserModel()
            {
                IsAuthenticated = isAuthenticated,
                Username = username
            };

            if (id > 0)
            {
                try
                {
                    IDelightServices service = new DelightServices();

                    AccountAvatarDTO info = service.GetUserStateInfo(id);
                    model.Remember = info.Remember;
                    if (model.Remember)
                    {
                        model.Username = info.Username;
                        model.Avatar = DataUriSchemeProvider.CreatePNGImage(info.Avatar);
                    }
                    if (model.IsAuthenticated)
                    {
                        model.Role = info.Role;
                    }

                }
                catch (RepositoryException)
                {
                }
            }

            return model;
        }
 public HttpResponseMessage Vote([FromBody] VoteModel model)
 {
     return SafeAction(() =>
     {
         IDelightServices service = new DelightServices();
         int remainingPoints = service.Vote(User.Identity.Name, model.ProductId, model.Points);
         var response = Request.CreateResponse<int>(HttpStatusCode.OK, remainingPoints);
         return response;
     }, model);
 }