Ejemplo n.º 1
0
        public async Task <IActionResult> PostMessage([FromBody] ResourceDocument document)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                var token   = SiteAuthorizationExtensions.GetTokenFromHeader(Request.Headers["Authorization"]);
                var userid  = SiteAuthorizationExtensions.DecryptUserId(token);
                var message = JsonApiUtil.GetMessageFromMesasgesPostRequest(document);

                if (string.IsNullOrWhiteSpace(message))
                {
                    return(StatusCode(422, new { title = "Field data error." }));
                }

                var messageRecord = new MessageRecord()
                {
                    UserId    = int.Parse(userid),
                    Message   = message,
                    CreatedAt = DateTime.Now
                };
                _context.MessageRecord.Add(messageRecord);
                await _context.SaveChangesAsync();

                messageRecord.User = _context.User.FirstOrDefault(u => u.Id == messageRecord.UserId);

                var newToken = SiteAuthorizationExtensions.CreateJwtToken(userid);
                this.Response.Headers.Add("Authorization", "Bearer " + newToken);

                var currentRequestUrl = this.HttpContext.Request.GetUri();

                // Build new document.
                var newDocument = JsonApiUtil.WriteDocumentForMessagesPostResponse(currentRequestUrl, messageRecord);
                return(Created(currentRequestUrl.AbsoluteUri, newDocument));
            }
            catch (Exception e)
            {
                return(StatusCode(500, new { title = "Something went wrong, please try again." }));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([FromBody] ResourceDocument document)
        {
            try
            {
                var username = CreateRandomUsername();

                var user = new User()
                {
                    UserName = username
                };
                _context.User.Add(user);
                await _context.SaveChangesAsync();

                var token = SiteAuthorizationExtensions.CreateJwtToken(user.Id.ToString());

                this.Response.Headers.Add("Authorization", "Bearer " + token);


                var session = new Session()
                {
                    Id        = user.Id,
                    CreatedAt = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"),
                    User      = user
                };

                var currentRequestUrl = this.HttpContext.Request.GetUri();

                // Build new document.
                var newDocument = JsonApiUtil.WriteDocumentForSessionsPostResponse(currentRequestUrl, session);
                return(Created(currentRequestUrl.AbsoluteUri, newDocument));
            }
            catch (Exception e)
            {
                return(StatusCode(500, new { title = "Something went wrong, please try again." }));
            }
        }