Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controller ve service birbirinden ayrılmalı #65

Open
saracalihan opened this issue Sep 5, 2021 · 1 comment · Fixed by #67
Open

controller ve service birbirinden ayrılmalı #65

saracalihan opened this issue Sep 5, 2021 · 1 comment · Fixed by #67
Assignees
Labels
invalid This doesn't seem right

Comments

@saracalihan
Copy link
Member

saracalihan commented Sep 5, 2021

Router içinde veritabanı ile ilgili business logic yapılmamalı bunun yerine ilgili işlemler fonksiyon haline getirilip service olarak kullanılmalı.

// src/services/content.js
const getContent = (slug) => {
try {
    const content = await models.contents.findOne({
      where: {
        slug,
      },
      include: [
        {
          model: models.images,
          as: 'image',
        },
        {
          model: models.users,
          as: 'user',
        },
      ],
    });

    if (!content) {
      return {
        errors: [
          {
            message: 'Content not found or you don\'t have a permission!',
          },
        ],
      };
    }
    content.views += 1;
    await content.save();
    return content;
  } catch (err) {
    return {
      errors: [
        {
          message: err.message,
        },
      ],
    };
  }
};

 const ContentService = {
  getContent,
};
export default ContentService;

Service içinde sadece veritabanı ile konuşma fonksiyonun ismine göre veritabanı ile konuşma işlemi yapılmalıdır. Alakasız hiçbir işlem yapılmamalıdır.

// src/routes/content.js
import ContentService from '../services/content.js';

// ...

const updateContentSchema = {
  body: Joi.object({
    name: Joi.string()
      .max(50),
    type: Joi.string()
      .min(0)
      .max(10),
    description: Joi.string()
      .max(250),
    image_path: Joi.string()
      .max(250),
  }),
};

// ...

const update = (req, res) =>{ // Controller
const { error } = updateContentSchema.body.validate(req.body);
  if (error) {
    return res.status(400).send({
      errors: error.details,
    });
  }

  const { slug } = req.params;
  try{
    let content = await ContentService.getContent(slug);
    await ContentService.updateContent(updateableFieldsFromBody, content.id);
  } catch(error){
    next(error);
  };
};

export default {
  prefix: '/contents',
  inject: (router) => {
    // ...
    router.put('/:slug', update);
  },
};
@saracalihan
Copy link
Member Author

Örnek kod için --> #67

@saracalihan saracalihan linked a pull request Sep 7, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant