public async Task <IHttpActionResult> DeleteRefractory([FromUri] int refId)  //optional
        {
            var authtor = await _uow.UserInfoService.GetUserById(User.Identity.GetUserId <int>());

            if (authtor == null)
            {
                return(this.Unauthorized());
            }

            if (authtor.IsBlocked)
            {
                return(BadRequest("Your account blocked."));
            }

            DTORefractory refractory = await _uow.RefractoryService.GetRefractoryByRefId(refId);

            if (refractory == null)
            {
                return(NotFound());
            }

            if (refractory.UserInfo.Id != authtor.Id)
            {
                return(BadRequest("It is not your refractory."));
            }

            if (!this.ModelState.IsValid)
            {
                return(BadRequest(this.ModelState));
            }

            await _uow.RefractoryService.DeleteRefractory(refId);

            return(Ok("Refractory removed"));
        }
        public async Task <IHttpActionResult> UnblockRefractory([FromUri] int refId)
        {
            DTORefractory refractory = await _uow.RefractoryService.GetRefractoryByRefId(refId);

            if (refractory == null)
            {
                return(NotFound());
            }

            if (!refractory.IsBlocked)
            {
                return(BadRequest("Refractory not blocked."));
            }

            var userId = this.User.Identity.GetUserId();

            if (userId == null || !User.IsInRole("admin"))
            {
                return(this.Unauthorized());
            }


            await _uow.ModeratorService.UnblockRefractory(refId);

            return(Ok("Refractory unblocked"));
        }
        public async Task <IHttpActionResult> EditRefractory([FromUri] int refId, [FromBody] RefractoryEditViewModel uRef)
        {
            var authtor = await _uow.UserInfoService.GetUserById(User.Identity.GetUserId <int>());

            if (authtor == null)
            {
                return(this.Unauthorized());
            }

            if (authtor.IsBlocked)
            {
                return(BadRequest("Your account blocked."));
            }

            DTORefractory refractory = await _uow.RefractoryService.GetRefractoryByRefId(refId);

            if (refractory == null)
            {
                return(NotFound());
            }

            if (refractory.UserInfo.Id != authtor.Id)
            {
                return(BadRequest("It is not your refractory."));
            }

            if (refractory.IsBlocked)
            {
                return(BadRequest("Refractory blocked."));
            }

            refractory.RefractoryDescription = uRef.RefractoryDescription;
            refractory.RefractoryType        = uRef.RefractoryType;
            refractory.RefractoryBrand       = uRef.RefractoryBrand;
            refractory.LastEdit = DateTime.Now;

            refractory.Density            = uRef.Density;
            refractory.MaxWorkTemperature = uRef.MaxWorkTemperature;
            refractory.Lime     = uRef.Lime;
            refractory.Alumina  = uRef.Alumina;
            refractory.Silica   = uRef.Silica;
            refractory.Magnesia = uRef.Magnesia;
            refractory.Carbon   = uRef.Carbon;
            refractory.Price    = uRef.Price;

            refractory.UserInfo = null;
            refractory.Comments = null;

            await _uow.RefractoryService.EditRefractory(refractory);

            return(Ok("Refractory edited"));
        }
        public async Task <IHttpActionResult> AddRefractory()
        {
            var authtor = await _uow.UserInfoService.GetUserById(User.Identity.GetUserId <int>());

            if (authtor == null)
            {
                return(this.Unauthorized());
            }

            if (authtor.IsBlocked)
            {
                return(BadRequest("Your account blocked."));
            }

            var            httpRequest = HttpContext.Current.Request;     //get request object
            HttpPostedFile refFile     = httpRequest.Files["RefPicture"]; //Gets the collection of files uploaded by the client, in multipart MIME format.

            if (refFile == null || refFile.ContentLength < 2)
            {
                return(BadRequest("No image"));
            }

            string filename = AddImage(refFile);  //add image for post


            DTORefractory newRefractory = new DTORefractory
            {
                RefractoryDescription = "", //descriptionRef Body (send next step EditRefractory by Id)
                RefractoryType        = httpRequest.Params["Type"],
                RefractoryBrand       = httpRequest.Params["Brand"],
                DateCreate            = DateTime.Now,
                UserInfoId            = authtor.Id,
                RefractoryPicture     = filename,
                Density            = float.Parse(httpRequest.Params["Density"]),
                MaxWorkTemperature = float.Parse(httpRequest.Params["MaxWorkTemperature"]),
                Lime     = float.Parse(httpRequest.Params["Lime"]),
                Alumina  = float.Parse(httpRequest.Params["Alumina"]),
                Silica   = float.Parse(httpRequest.Params["Silica"]),
                Magnesia = float.Parse(httpRequest.Params["Magnesia"]),
                Carbon   = float.Parse(httpRequest.Params["Carbon"]),
                Price    = float.Parse(httpRequest.Params["Price"])
            };

            if (!this.ModelState.IsValid)
            {
                return(BadRequest(this.ModelState));
            }

            await _uow.RefractoryService.AddRefractory(newRefractory);

            return(Content(HttpStatusCode.Created, "Refractory added"));
        }
        public async Task EditRefractory(DTORefractory dtoRefractory)
        {
            var refractory = AutoMapper.Mapper.Map <DTORefractory, Refractory>(dtoRefractory);

            if (refractory.UserInfoId != 0)
            {
                await _uow.Refractory.Update(refractory);
            }
            else
            {
                throw new ArgumentException("Wrong data");
            }
        }
        public async Task AddRefractory(DTORefractory dtoRefractory)
        {
            var refractory = AutoMapper.Mapper.Map <DTORefractory, Refractory>(dtoRefractory);

            if (refractory.RefractoryDescription != null)
            {
                await _uow.Refractory.Insert(refractory);
            }
            else
            {
                throw new ArgumentException("Wrong data");
            }
        }
        public async Task <IHttpActionResult> AddComment([FromUri] int refId)
        {
            var    httpRequest = HttpContext.Current.Request;
            string comment     = httpRequest.Params["Coment"];

            DTORefractory refractory = await _uow.RefractoryService.GetRefractoryByRefId(refId);

            if (refractory == null)
            {
                return(NotFound());
            }

            var userId = this.User.Identity.GetUserId();

            if (userId == null)
            {
                return(this.Unauthorized());
            }

            if (comment.Length < 1)
            {
                ModelState.AddModelError("Body", "Please write comment.");
            }

            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            DTOUser user = await _uow.UserInfoService.GetUserById(User.Identity.GetUserId <int>());

            DTOComment dtoComment = new DTOComment {
                DateCreation = DateTime.Now, RefractoryId = refId, UserInfoId = user.Id, CommentBody = comment
            };

            await _uow.CommentService.AddComment(dtoComment);

            return(Content(HttpStatusCode.Created, "Comment added."));
        }
        public async Task <IHttpActionResult> GetComments([FromUri] int refId)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            DTORefractory refractory = await _uow.RefractoryService.GetRefractoryByRefId(refId);

            if (refractory == null)
            {
                return(NotFound());
            }

            if (refractory.IsBlocked)
            {
                return(BadRequest("This refractory blocked"));
            }

            var userId = User.Identity.GetUserId();

            if (userId == null)
            {
                return(this.Unauthorized());
            }

            List <CommentViewModel> comments = AutoMapper.Mapper.Map <IEnumerable <DTOComment>, List <CommentViewModel> >(
                await _uow.CommentService.GetCommentsForRefractory(refId));

            if (comments != null)
            {
                return(Ok(comments));
            }
            else
            {
                return(NotFound());
            }
        }
        public async Task <IHttpActionResult> Delete([FromUri] int commentId)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            if (User.Identity.GetUserName() == null)
            {
                return(this.Unauthorized());
            }

            var userId = User.Identity.GetUserId <int>();

            var comment = await _uow.CommentService.GetCommentById(commentId);

            if (comment == null)
            {
                return(NotFound());
            }

            DTORefractory post = await _uow.RefractoryService.GetRefractoryByRefId(comment.RefractoryId);

            if (post == null)
            {
                return(NotFound());
            }

            if (comment.UserInfoId != userId)
            {
                return(BadRequest("It is not your comment"));
            }

            await _uow.CommentService.DeleteComment(commentId);

            return(Ok("Comment deleted"));
        }