Example #1
0
        public JsonResult Put([FromBody] NurseryViewModel vm)
        {
            try
            {
                var matchingNurs = User.FindAll("Nursery").FirstOrDefault(claim => claim.Value == vm.Id.ToString());

                if (User.IsInRole("Admin") || matchingNurs != null)
                {
                    var nursery = Mapper.Map <Nursery>(vm);
                    nursery.Address    = vm.Address;
                    nursery.Modified   = DateTime.Now;
                    nursery.ModifiedBy = User.Identity.Name;

                    _repository.SaveNursery(nursery);
                    _repository.SaveAddress(nursery.Address);

                    if (_repository.SaveAll())
                    {
                        return(Json(Mapper.Map <NurseryViewModel>(nursery)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save nursery", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new
                {
                    Message = ex.Message
                }));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }
Example #2
0
        public JsonResult Post([FromBody] NoteViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var note = Mapper.Map <Note>(vm);
                    note.Created   = DateTime.Now;
                    note.CreatedBy = User.Identity.Name;

                    _repository.AddNote(note);

                    if (_repository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <NoteViewModel>(note)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new note", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to save new note"));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json("Validation failed on new employee"));
        }
Example #3
0
        public JsonResult Post(int nurseryId, [FromBody] EmployeeViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var employee = Mapper.Map <Employee>(vm);

                    var matchingNurs = User.FindAll("Nursery").FirstOrDefault(claim => claim.Value == nurseryId.ToString());
                    if (User.IsInRole("Admin") || matchingNurs != null)
                    {
                        employee.Created    = DateTime.Now;
                        employee.CreatedBy  = User.Identity.Name;
                        employee.Modified   = DateTime.Now;
                        employee.ModifiedBy = User.Identity.Name;
                        employee.NurseryId  = nurseryId;

                        _repository.AddEmployee(employee);

                        if (_repository.SaveAll())
                        {
                            Response.StatusCode = (int)HttpStatusCode.Created;
                            return(Json(Mapper.Map <EmployeeViewModel>(employee)));
                        }
                    }
                    else
                    {
                        Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        return(Json("Unauthorized to create employees in this nursery"));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new employee", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to save new employee"));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json("Validation failed on new employee"));
        }
Example #4
0
        public JsonResult Post(int nurseryId, [FromBody] ClassViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newClass = Mapper.Map <Class>(vm);

                    var matchingNurs = User.FindAll("Nursery").FirstOrDefault(claim => claim.Value == nurseryId.ToString());
                    if (User.IsInRole("Admin") || matchingNurs != null)
                    {
                        newClass.Created    = DateTime.Now;
                        newClass.CreatedBy  = User.Identity.Name;
                        newClass.Modified   = DateTime.Now;
                        newClass.ModifiedBy = User.Identity.Name;
                        newClass.NurseryId  = nurseryId;

                        _repository.AddClass(nurseryId, newClass);

                        if (_repository.SaveAll())
                        {
                            Response.StatusCode = (int)HttpStatusCode.Created;
                            return(Json(Mapper.Map <ClassViewModel>(newClass)));
                        }
                    }
                    else
                    {
                        Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        return(Json($"Unauthorized to create new class in nursery {nurseryId}"));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new class", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to save new class"));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json("Validation failed on new class"));
        }
Example #5
0
        public JsonResult Post([FromBody] ChildViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var child = Mapper.Map <Child>(vm);

                    var matchingNurs = User.FindAll("Nursery").FirstOrDefault(claim => claim.Value == _repository.GetClassNurseryId((int)vm.ClassId).ToString());
                    if (User.IsInRole("Admin") || matchingNurs != null)
                    {
                        child.Created    = DateTime.Now;
                        child.CreatedBy  = User.Identity.Name;
                        child.Modified   = DateTime.Now;
                        child.ModifiedBy = User.Identity.Name;

                        _repository.AddChild(child);

                        if (_repository.SaveAll())
                        {
                            Response.StatusCode = (int)HttpStatusCode.Created;
                            return(Json(Mapper.Map <ChildViewModel>(child)));
                        }
                    }
                    else
                    {
                        Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        return(Json("Unauthorized to create new child in this nursery"));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new child", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to save new child"));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json("Validation failed on new child"));
        }