Exemple #1
0
 public Result <ClassDTO> CreateClass(ClassDTO model)
 {
     try
     {
         return(classService.CreateClass(model));
     }
     catch (Exception ex)
     {
         return(new Result <ClassDTO>(true, ex.Message, null));
     }
 }
 public async Task<IActionResult> Create(CreateClassRequest request)
 {
     if (!ModelState.IsValid)
     {
         return BadRequest(ModelState);
     }
     var result = await _classService.CreateClass(request);
     if (result.IsSuccessed == false)
     {
         return BadRequest(result);
     }
     return Ok(result);
 }
        public async Task <IActionResult> CreateClass([FromBody] ClassResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var result = await classService.CreateClass(resource);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                logger.LogError(1003, ex, ex.Message);
            }

            return(BadRequest());
        }
        public ActionResult CreateClass(ClassModel model)
        {
            if (ModelState.IsValid)
            {
                // check if foreign key(s), unique key(s) are invalid
                if (!IsCreateKeysValid(model))
                {
                    return(StatusCode(resp.code, resp));
                }

                Class aClass = new Class
                {
                    Name          = model.Name,
                    Year          = model.Year,
                    GradeID       = model.GradeID,
                    HeadTeacherID = model.HeadTeacherID
                };

                _classService.CreateClass(aClass);

                Console.WriteLine(aClass.Name);
                Console.WriteLine(aClass.Year);
                Console.WriteLine(aClass.Size);
                Console.WriteLine(aClass.GradeID);
                Console.WriteLine(aClass.HeadTeacherID);

                return(StatusCode(201, aClass)); // 201: Created
            }
            else
            {
                var errors = new List <string>();
                foreach (var state in ModelState)
                {
                    foreach (var error in state.Value.Errors)
                    {
                        errors.Add(error.ErrorMessage);
                    }
                }
                return(BadRequest(errors));
            }
        }
Exemple #5
0
        public async Task <IActionResult> Create(ClassModel model)
        {
            DataResult <ClassModel> result = await _classService.CreateClass(model);

            if (result.Success)
            {
                return(RedirectToAction("Details", new { result.Data.Id }));
            }

            if (result.ErrorCode == ErrorCode.UniquenessError)
            {
                ViewBag.Phylums = new SelectList(
                    (await _phylumRepository.GetAll()).OrderBy(e => e.Denomination),
                    nameof(Class.Id),
                    nameof(Class.Denomination));

                ModelState[nameof(model.Denomination)].Errors.Add("Such a record already exists");
                return(View("Create", model));
            }

            return(RedirectToAction("Error", "Error", new { result.ErrorCode, modelName = nameof(Class) }));
        }
 public async Task <ActionResult <int> > Create([FromBody] Class newClass)
 {
     return(await _classService.CreateClass(newClass));
 }