Beispiel #1
0
        public async Task <IActionResult> PutDiary(string id, DiaryResource diary)
        {
            if (id != diary.Id || !DiaryExists(id))
            {
                return(BadRequest());
            }
            var result = await _context.Diaries.FindAsync(id);

            string userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var    user   = await _context.Users.FindAsync(userId);

            if (!user.Equals(result.user))
            {
                return(Unauthorized());
            }
            result.name   = diary.Name;
            result.edited = DateTime.Now;
            _context.Entry(result).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DiaryExists(id))
                {
                    return(NotFound());
                }
                throw;
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <ActionResult <DiaryResource> > GetDiary(string id)
        {
            var diary = await _context.Diaries.FindAsync(id);

            if (diary == null)
            {
                return(NotFound());
            }
            string userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var    user   = await _context.Users.FindAsync(userId);

            if (!user.Equals(diary.user))
            {
                return(Unauthorized());
            }
            //var result = new DiaryResource(diary.id, diary.name, diary.pages.Count(), diary.created, diary.edited);
            var result = new DiaryResource(diary.id, diary.name, diary.created, diary.edited);

            return(Ok(result));
        }
Beispiel #3
0
        public async Task <ActionResult <DiaryResource> > PostDiary([FromBody] string name)
        {
            string userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            var    user   = await _context.Users.FindAsync(userId);

            var diary = new Diary {
                name = name, created = DateTime.Now, user = user
            };

            diary.edited = diary.created;
            //diary.user = from token
            _context.Diaries.Add(diary);
            await _context.SaveChangesAsync();

            //var result = new DiaryResource(diary.id, diary.name, diary.pages.Count(), diary.created, diary.edited);

            var result = new DiaryResource(diary.id, diary.name, diary.created, diary.edited);

            return(Ok(result));
            //return CreatedAtAction("GetDiary", new { Id = diary.id }, diary);
        }