示例#1
0
        public async Task <IActionResult> EditTest(int testId, TestForAddingDto testForEditDto)
        {
            var test = await _context.Tests.FirstOrDefaultAsync(t => t.Id == testId);

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

            if (test.UserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            test.Name             = testForEditDto.Name;
            test.ActiveToDateTime = testForEditDto.ActiveToDateTime;
            test.Time             = testForEditDto.Time;
            test.Note             = testForEditDto.Note;
            test.Level            = testForEditDto.Level;
            test.CategoryId       = testForEditDto.CategoryId;
            test.DateChanged      = DateTime.Now;

            if (await _context.SaveChangesAsync() > 0)
            {
                return(Ok(test));
            }

            return(BadRequest());
        }
示例#2
0
        public async Task <IActionResult> AddTest(TestForAddingDto testForAddingDto)
        {
            //Мапване с категорията
            var category = await _context.Categories.FirstOrDefaultAsync(c => c.Id == testForAddingDto.CategoryId);

            //Test class
            var testToAdd = _mapper.Map <Test>(testForAddingDto);

            //Мапване с логнатият потребител
            testToAdd.User = await _userManager.FindByNameAsync(User.FindFirst(ClaimTypes.Name).Value);

            testToAdd.Category = category;

            await _context.Tests.AddAsync(testToAdd);

            if (await _context.SaveChangesAsync() > 0)
            {
                //Връщане на нововъденият тест
                return(Ok(new
                {
                    testId = testToAdd.Id
                }));
            }

            return(BadRequest());
        }