예제 #1
0
        public async Task <IHttpActionResult> Create([FromBody, CustomizeValidator(RuleSet = "AddComment, default")] CommentWeb commentWeb)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var    sub       = (User as ClaimsPrincipal).FindFirst("sub");
                string subString = sub.Value;
                var    user      = (await Uservice.GetByIdAsync(subString));
                if (user.IsSuccess == true)
                {
                    var userId = user.Data;

                    var commentlogic = mapper.Map <CommentWeb, CommentLogic>(commentWeb);
                    commentlogic.User = userId;
                    var result = await service.AddAsync(commentlogic);

                    if (result.IsSuccess == true)
                    {
                        await bus.SendAsync("Podcasts", $"Added Comment to {commentlogic.Course.Name} by {userId.Name}");

                        return(Ok(commentlogic));
                    }
                    else
                    {
                        return(BadRequest(result.Message));
                    }
                }
                else
                {
                    return(Unauthorized());
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
예제 #2
0
        public async Task <IHttpActionResult> Update(string id, [FromBody, CustomizeValidator(RuleSet = "UpdateComment, default")] CommentWeb commentWeb)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var    sub       = (User as ClaimsPrincipal).FindFirst("sub");
                string subString = sub.Value;
                var    user      = (await Uservice.GetByIdAsync(subString));
                if (user.IsSuccess == true)
                {
                    var userId = user.Data;

                    var commentlogic = mapper.Map <CommentWeb, CommentLogic>(commentWeb);
                    var result       = await service.UpdateAsync(commentlogic, userId);

                    if (result.IsSuccess == true)
                    {
                        return(Ok(commentlogic));
                    }
                    else
                    {
                        return(BadRequest(result.Message));
                    }
                }
                else
                {
                    return(Unauthorized());
                }
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }